Flag | Usage |
---|---|
-d --detach | run container in the background and give the shell prompt back |
down | |
up |
CONTAINER ID IMAGE c031860c837e sonatype/nexus:2.14.4-03
(returns a wealth of JSON data)
__ID__
/g' -e 's|:/|\n\t /|g' -e 's/^ {16}//'__ID__
"LowerDir": "/var/lib/docker/overlay2/__ID__
-init/diff /var/lib/docker/overlay2/__ID__
/diff /var/lib/docker/overlay2/__ID__
/diff /var/lib/docker/overlay2/__ID__
/diff /var/lib/docker/overlay2/__ID__
/diff /var/lib/docker/overlay2/__ID__
/diff", "MergedDir": "/var/lib/docker/overlay2/__ID__
/merged", "UpperDir": "/var/lib/docker/overlay2/__ID__
/diff",
volumeto refer both to volumes and to bind mounts, which makes things even harder for newbies
As opposed to bind mounts, all options for volumes are available for both --mount and -v flags., which translates to :
all options for volumes are available for both --mount and -v flags: you may define a volume using either --mount or --volume, you'll have access to the same options
As opposed to bind mounts,: it's the contrary for bind mounts : "not all options are available for both --mount and -v flags" (... and they let us guess ??? )
bind mount | volume | ||
---|---|---|---|
anonymous | named | ||
CLI | -v /path/on/host:/path/in/container |
-v path/in/container
|
|
Dockerfile | You can't (details) :
|
VOLUME path/in/container
|
You can't. Otherwise, 2 containers instantiated from the same image would try to create 2 volumes with the same name, which is not possible (details : 1, 2). |
docker-compose.yml | version: '3.7' services: db: volumes: - ./db:/var/lib/mysql wp: volumes: - ./wpb:/var/www/html(source) |
version: '3.1' services: drupal: image: drupal:8.2-apache ports: - 8080:80 volumes: - /var/www/html/modules -(source) |
version: "3" services: db: image: db volumes: #1 use the named volume - myvol:/var/lib/db myvol created with #3 backup: image: backup-service volumes: #2 use the named volume - myvol:/var/lib/backupmyvol created with #3 volumes: #3 create the named myvol: volume myvol(sources : 1, 2) |
version: '3.8' services: myService: build: context: './path/to/myService-7.3.1/' image: myService:7.3.1other example
build
and image: imageName:imageTag
, imageName:imageTag refers to the image built by Compose, not to the base image. elastic:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
stop_grace_period: '5m'
ports:
- '9207:9200'
volumes:
- 'elastic_data:/usr/share/elasticsearch/data:rw'
- './logs/elastic:/usr/share/elasticsearch/logs:rw'
env_file:
- './env/elastic.env'
cluster.name='talend7pp' ingest.geoip.downloader.enabled=false discovery.type='single-node' _JAVA_OPTIONS='-Xmx4g -Xms4g'
env_file
ports: - "hostPort:containerPort"
source:target:mode
volumes:
my_named_volume:
driver: local
ADD [--chown=kevin:developers] source destination
ADD
or COPY
?
ARG myVariable
ARG
must not appear before FROM$
prefix (like shell variables, source) :
ARG myVariable=42 RUN echo $myVariable
COPY
files into an existing directory :
COPY someFile /path/to/destination
COPY someFile /path/to/destination/
EXPOSE portNumber
states that something inside the container is listening on port portNumber.FROM
(Understand how ARG
and FROM
interact)AS newName
can be specified to refer to this new build stage in a subsequent FROM
FROM baseImage [AS newName]
FROM baseImage:tag [AS newName]
RUN
, CMD
and ENTRYPOINT
instructions that follow it in the DockerfileVOLUME /data
VOLUME /volume1 /volume2
VOLUME [ "/data" ]
VOLUME [ "/volume1", "/volume2" ]
VOLUME /dir1 /dir2
:
/
, VOLUME /data
and VOLUME data
may/may not refer to the same thing depending on circumstancesRUN
, CMD
, ENTRYPOINT
, COPY
and ADD
instructions that follow it in the DockerfileWORKDIR
doesn't exist, it will be created even if it's not used in any subsequent Dockerfile instruction.as ifwe were provisioning VMs + playing with them + destroying them when the test is over (this is actually what I've tried to achieve here). It is especially nice to play with software without breaking things.
Looks like both would fit pretty well with each other. So what's the problem ?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
docker-compose version 1.27.4, build 40524192
weband 2
DB. We'll use these with Ansible later.
version: '3.7' services: ubuntu_web1: hostname: web1 build: . this service uses the image built from the Dockerfile found in this directory (details) container_name: c_ubuntu_web1 tty: true ubuntu_web2: hostname: web2 build: . container_name: c_ubuntu_web2 tty: true ubuntu_db1: hostname: db1 build: . container_name: c_ubuntu_db1 tty: true ubuntu_db2: hostname: db2 build: . container_name: c_ubuntu_db2 tty: true
Building ubuntu_db2 Step 1/11 : FROM ubuntu:16.04 ---> 5e13f8dd4c1a Step 2/11 : ARG userName='ansible' ---> Using cache ---> 7f163145edfd Step 3/11 : ARG homeDir="/home/$userName" ---> Using cache ---> 9025b899c1df Step 4/11 : ARG sshDir="$homeDir/.ssh" ---> Using cache ---> b32d4b7d1abf Step 5/11 : ARG authorizedKeysFile="$sshDir/authorized_keys" ---> Using cache ---> 8477ab5bcac4 Step 6/11 : ARG publicSshKey='./ansible.pub' ---> Using cache ---> 5b2235eb9fbf Step 7/11 : RUN apt-get update && apt-get install -y iproute2 iputils-ping openssh-server && apt-get clean && useradd -d "$homeDir" -s /bin/bash -m "$userName" && mkdir -p "$sshDir" ---> Using cache ---> 41cfeaaa7dda Step 8/11 : COPY "$publicSshKey" "$authorizedKeysFile" this step doesn't like it when source files are symlinks ---> Using cache ---> 3dcab2010a2b Step 9/11 : RUN chown -R "$userName":"$userName" "$sshDir" && chmod 700 "$sshDir" && chmod 600 "$authorizedKeysFile" ---> Using cache ---> 1f511354cd72 Step 10/11 : EXPOSE 22 ---> Using cache ---> deb5199624a4 Step 11/11 : CMD [ "sh", "-c", "service ssh start; bash"] ---> Using cache ---> 3bb266505c4c Successfully built 3bb266505c4c Successfully tagged dockercompose_ubuntu_db2:latest WARNING: Image for service ubuntu_db2 was built because it did not already exist. To rebuild this image you must use docker-compose build or docker-compose up --build.Then :
Creating c_ubuntu_web1 ... done Creating c_ubuntu_db1 ... done Creating c_ubuntu_web2 ... done Creating c_ubuntu_db2 ... done Attaching to c_ubuntu_db1, c_ubuntu_web1, c_ubuntu_web2, c_ubuntu_db2 c_ubuntu_db1 | * Starting OpenBSD Secure Shell server sshd [ OK ] c_ubuntu_web1 | * Starting OpenBSD Secure Shell server sshd [ OK ] c_ubuntu_web2 | * Starting OpenBSD Secure Shell server sshd [ OK ] c_ubuntu_db2 | * Starting OpenBSD Secure Shell server sshd [ OK ](No prompt given at that time, so Ctrl-z + bg to get it back)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76f413236d70 dockercompose_ubuntu_db2 "sh -c 'service ssh …" 3 minutes ago Up 3 minutes 22/tcp c_ubuntu_db2 240e1505313e dockercompose_ubuntu_web2 "sh -c 'service ssh …" 3 minutes ago Up 3 minutes 22/tcp c_ubuntu_web2 b5b6887d2b02 dockercompose_ubuntu_db1 "sh -c 'service ssh …" 3 minutes ago Up 3 minutes 22/tcp c_ubuntu_db1 95f988a02ff2 dockercompose_ubuntu_web1 "sh -c 'service ssh …" 3 minutes ago Up 3 minutes 22/tcp c_ubuntu_web1
REPOSITORY TAG IMAGE ID CREATED SIZE dockercompose_ubuntu_db1 latest 3bb266505c4c 4 minutes ago 205MB dockercompose_ubuntu_db2 latest 3bb266505c4c 4 minutes ago 205MB dockercompose_ubuntu_web1 latest 3bb266505c4c 4 minutes ago 205MB dockercompose_ubuntu_web2 latest 3bb266505c4c 4 minutes ago 205MB
Creating c_ubuntu_db1 ... done Creating c_ubuntu_web1 ... done Creating c_ubuntu_web2 ... done Creating c_ubuntu_db2 ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76f413236d70 dockercompose_ubuntu_db2 "sh -c 'service ssh …" 2 hours ago Up 34 minutes 22/tcp c_ubuntu_db2
"db2"
{{json .Config.Hostname}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}
' 76f413236d70"db2" 172.18.0.5
{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{json .Config.Hostname}}
' "$containerId"; done | tr -d '"
' | sort172.18.0.2 db1 172.18.0.3 web2 172.18.0.4 web1 172.18.0.5 db2
{{json .Config.Hostname}} ansible_host={{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}
' "$containerId"; done | tr -d '"
' | sort[mysqlGroup] mysql ansible_host=172.18.0.2Both commands give the same result :
mysql
-m ping -u ansiblemysql | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
OKCheck :
pub rsa4096 2017-02-22 [SCEA] 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 uid [ unknown] Docker Release (CE deb) <docker@docker.com> sub rsa4096 2017-02-22 [S]
Unable to find image 'hello-world:latest' locally you can safely ignore this line since it's the 1st launch latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
For the impatientsprocedure, see the source documentation for details
mountedinto a container
VOLUME
in Dockerfilevolumes
in docker-compose.ymlCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f64391d2ae1b atlassian_confluence "./entrypoint.sh" 5 days ago Up 4 days 0.0.0.0:8090-8091->8090-8091/tcp atlassian_confluence_1
"Type": "volume", "Name": "atlassian_confluence_tmp", "Source": "/var/lib/docker/volumes/atlassian_confluence_tmp/_data", "Destination": "/tmp", "Driver": "local", "Mode": "rw", "RW": true, "Propagation": "" -- "Type": "volume", "Name": "atlassian_confluence_data", "Source": "/var/lib/docker/volumes/atlassian_confluence_data/_data", the Source is inside the container (see Mountpoint below) "Destination": "/var/atlassian/application-data/confluence", the Destination is on the host "Driver": "local", "Mode": "rw", "RW": true, "Propagation": ""
[ { "CreatedAt": "2020-10-08T23:09:51+02:00", "Driver": "local", "Labels": { "com.docker.compose.project": "atlassian", "com.docker.compose.version": "1.21.2", "com.docker.compose.volume": "confluence_data" }, "Mountpoint": "/var/lib/docker/volumes/atlassian_confluence_data/_data", "Name": "atlassian_confluence_data", "Options": null, "Scope": "local" } ]
sees/ contains everything inside the container from file system to processes etc. You can issue a ps on the host and see processes running inside the container (source).
Docker Root Dir
'Docker Root Dir: /var/lib/docker
REPOSITORY TAG IMAGE ID CREATED SIZE
whatever/myImage latest e00a21e210f9 22 months ago 19.2MB
(Lower|Merged|Upper|Work)Dir
' | sed 's|/var|\n/var|g'"LowerDir": " /var/lib/docker/overlay2/9ceb16316d05119812856edda1c772b3680ff20336859cb79e8d58df8abf787a/diff: /var/lib/docker/overlay2/ac535e1ef985bec3d5bc90a5124f4ca14a610b9f007966f7521496aa6b6866ac/diff: /var/lib/docker/overlay2/4eeff3b3fc7a14b197827ffae0cab33c0df6a15b08b2f45895c6e987a6f3013a/diff", "MergedDir": " /var/lib/docker/overlay2/6923b6f343bd98e0a05826de6794dcb1756b4eb5fad9811fcad773e76b66a737/merged", "UpperDir": " /var/lib/docker/overlay2/6923b6f343bd98e0a05826de6794dcb1756b4eb5fad9811fcad773e76b66a737/diff", "WorkDir": " /var/lib/docker/overlay2/6923b6f343bd98e0a05826de6794dcb1756b4eb5fad9811fcad773e76b66a737/work"