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
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 v2.29.1
cat << EOF url1 : $urlGithub1 url2 : $urlGithub2 EOF
v
of dockerComposeVersion's vX.Y.Z
is/is not included in the path. Try with/without.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"
}
Flag | Usage |
---|---|
-d --detach | run container in the background and give the shell prompt back |
down | |
up |