Docker Run Options in a Docker Compose File? - docker

I have what is prob a Docker Compose 101 question. I can spin up a simple Docker container with docker run:
me#host1:~/dc$
me#host1:~/dc$ sudo docker run -dit --name myContainer 54c9d81cbb44
60d254728f0a763bdda3078bd1c708176ca21e3eced475cb7e2c3edc7859a12c
me#host1:~/dc$
me#host1:~/dc$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60d254728f0a 54c9d81cbb44 "bash" 2 minutes ago Up 2 minutes myContainer
me#host1:~/dc$
Easy! So then, I translate the above into a docker-compose.yaml file:
version: "2"
services:
myService:
container_name: myContainer2
image: 54c9d81cbb44
When I run the above file, the container exits immediately:
me#host1:~/dockerCompose$
me#host1:~/dockerCompose$ sudo docker-compose up
Creating myContainer2 ...
Creating myContainer2 ... done
Attaching to myContainer2
myContainer2 exited with code 0
me#host1:~/dockerCompose$
me#host1:~/dockerCompose$
me#host1:~/dockerCompose$ sudo docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
110f648fadbe 54c9d81cbb44 "bash" 14 seconds ago Exited (0) 13 seconds ago myContainer2
60d254728f0a 54c9d81cbb44 "bash" 6 minutes ago Up 6 minutes myContainer
me#host1:~/dockerCompose$
(The above happens even when I try to spin up myContainer2 without myContainer running.)
So what gives? I'm tempted to say the docker run "-dit" options are what are making the difference here; that is the only difference I see between the docker run and docker-compose versions. I've been Googling for "How to set docker run options in docker-compose file" for an hour, but pulling up information that isn't applicable here. I'm clearly missing something fundamental. Anyone see what? Thank you.

If you set stdin_open and tty to true, it'll stay up. Those are equivalent to the i and t options.
version: "2"
services:
myService:
container_name: myContainer2
image: 54c9d81cbb44
stdin_open: true
tty: true
Start the container with docker-compose up -d to run it detached.

Related

GitHub Actions Running jobs in a container with a privileged and mount option

In the Github actions workflow, I am trying to reproduce the following step:
docker run -d --name testmachine --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro jrei/systemd-ubuntu:20.04
It does not seem to be working as expected when I configure it on the Github actions workflow
name: test
on:
workflow_dispatch:
push:
jobs:
test:
name: test
runs-on: self-hosted
container:
image: jrei/systemd-ubuntu:20.04
volumes:
- "/sys/fs/cgroup:/sys/fs/cgroup:ro"
options: --privileged
When I do docker ps on the first step, I get the following (which is good - you can see /lib/systemd/systemd in the command):
PS C:\Windows\system32> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efe232522c68 jrei/systemd-ubuntu:20.04 "/lib/systemd/systemd" 59 minutes ago Up 59 minutes testmachine
And on the second step I am getting the following (github actions workflow):
50668b9d83fc jrei/systemd-ubuntu:20.04 "tail -f /dev/null" 16 minutes ago Up 16 minutes 8fe5922c3
Any Idea what I am doing wrong?
Thanks

Behat container keeps exiting

I'm using the following code to create a Behat docker container:
version: '3'
services:
behat:
container_name: behat.test
image: docksal/behat
# command: tail -F anything
# tty: true
# ports:
# - 4444:4444
# restart: always
But I'm experiencing a persistent problem with the container continually exiting with a code 1 as a result I can't interact with the container.
All the commented out portions of the code are what I have tried to resolve the issue.
Here is the output for the Behat container when I run docker-compose up -d --build:
d8c515771e4d docksal/behat "behat" 6 seconds ago Exited (1) 5 seconds ago behat.test
Update
I found Behat was reporting the following error :
`FeatureContext` context class not found and can not be used.
FeatureContext context class not found and can not be used.
This means behat fails to find features which needed, you could use behat --init to init a one.
$ docker run --rm -v $(pwd):/src docksal/behat --init
+d features - place your *.feature files here
+d features/bootstrap - place your context classes here
+f features/bootstrap/FeatureContext.php - place your definitions, transformations and hooks here
Then in your host, there will be a features folder, then run the command again see it's ok now:
$ docker run --rm -v $(pwd):/src docksal/behat
No scenarios
No steps
0m0.00s (7.73Mb)
For docker-compose, it's same, you need to assure there is features mount to the container. You could reference official folder structure to have your design, a workable step as next:
In current folder:
docker run --rm -v $(pwd):/src docksal/behat --init
Write a docker-compose.yaml like next:
version: "2.1"
services:
# Behat
behat:
hostname: behat
image: ${BEHAT_IMAGE:-docksal/behat}
volumes:
- .:/src
# Run a built-in web server for access to HTML reports
ports:
- 8000:8000
entrypoint: "php -S 0.0.0.0:8000"
docker-compose up -d
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
55f506afe31a docksal/behat "php -S 0.0.0.0:8000" 29 seconds ago Up 25 seconds 0.0.0.0:8000->8000/tcp 2020121502_behat_1
$ docker logs 2020121502_behat_1
PHP 7.3.25 Development Server started at Tue Dec 15 05:39:00 2020

Can not start Docker container

I created ubuntu image using docker-compose. Here is the relevant code from docker-compose.yml:
ubuntu-os:
container_name: ubuntu
image: ubuntu
volumes:
- ubuntu-datavolume:/home/username/docker/.os/ubuntu/
volumes:
ubuntu-datavolume:
It gets stopped as soon as it is started. I can not interract with the container. Here is relevant docker ps -a:
03dae5416b67 ubuntu "/bin/bash" 12 minutes ago Exited (0) 3 minutes ago ubuntu
I have tried every possible combo of docker start -a ubuntu but with no luck. I want this image to persist data across restart so I created the volume. Any suggestions?
Creating a new container is not what I am looking for but to start the existing container. I don't want to run the container but start and interact.
You use a ubuntu image which have an entry point /bin/sh. If you launch this without interactive/terminal linked, it will just run and exit with code 0. Your container finish successfully.
You can add the option:
stdin_open: true
tty: true
Referenced https://docs.docker.com/compose/compose-file/#domainname-hostname-ipc-mac_address-privileged-read_only-shm_size-stdin_open-tty-user-working_dir
or add command line that do something. like: command: sleep 600000

docker exec not working in docker-compose containers

I'm executing two docker containers using docker compose.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eef95ca1b59b gogent_qaf "/bin/sh -c ./slave.s" 14 seconds ago Up 12 seconds 4242/tcp, 7000-7005/tcp, 9999/tcp, 0.0.0.0:30022->22/tcp coreqafidm_qaf_1
a01373e893eb gogent_master "/bin/sh -c ./master." 15 seconds ago Up 13 seconds 4242/tcp, 0.0.0.0:27000->7000/tcp, 0.0.0.0:27001->7001/tcp, 0.0.0.0:27002->7002/tcp, 0.0.0.0:27003->7003/tcp, 0.0.0.0:29999->9999/tcp coreqafidm_master_1
When I try to use:
docker exec -it coreqafidm_qaf_1 /bin/bash
I get the error:
docker exec -it coreqafidm_qaf_1 /bin/bash
no such file or directory
Here is the docker-compose file:
version: '2'
services:
master:
image: gogent_master
volumes:
- .:/d1/apps/qaf
- ./../core-idm-gogent/:/d1/apps/gogent
ports:
- "27000-27003:7000-7003"
- "29999:9999"
build:
context: .
dockerfile: Dockerfile.master
qaf:
image: gogent_qaf
ports:
- "30022:22"
volumes:
- .:/d1/apps/qaf
- ./../core-idm-gogent/:/d1/apps/gogent
depends_on: [master]
build:
context: .
dockerfile: Dockerfile.qaf
Both Docker files involved have as their last WORKDIR command:
WORKDIR /d1/apps/qaf
If there is a REAL directory /d1/apps/qaf on the machine's natural file system docker exec works, to some degree. It will open up a shell. However, the mapped in volumes are not available to this shell and the files I see are the ones in the real, natural directory, not what should be the mapped in volume.
$ mkdir /d1/apps/qaf
$ docker exec -it coreqafidm_qaf_1 /bin/bash
root#eef95ca1b59b:/d1/apps/qaf#
root#eef95ca1b59b:/d1/apps/qaf# ls /d1/apps/gogent
ls: cannot access /d1/apps/gogent: No such file or directory
The volumes work correctly from within the docker-compose context. I have scripts executing in their and they work. It's just docker exec that fails to see the volumes.
The error stems from a the container not finding /bin/bash, hence the no such file or directory error. The docker exec works fine though.
Try with /bin/sh.
Well, I installed docker-compose etc. on a different machine and this problem was not there. Go figure. This is just one of those things I don't have time to track down.

Running copies of the same multi-container app with Docker Compose

Problem
I want to run a webapp via Docker by running 2 containers as a unit.
1 container runs my web-server (Tomcat 7).
The other container runs my database (Postgres 9.4).
I can run docker-compose up and Docker is able to spin up my two containers as specified in my docker-compose.yml:
web:
build: .
ports:
- "5000"
links:
- db
db:
image: postgres
I'd like to be able to spin up another copy of my webapp by running docker-compose up again, but this results in Docker telling me that there are already containers running:
$ docker-compose up -d
Creating composetest_db_1
Creating composetest_web_1
$ docker-compose up -d
composetest_db_1 is up-to-date
composetest_web_1 is up-to-date
My work around
I've gotten around this issue by using the -p option to give new copies different project names:
$ docker-compose -p project1 up -d
...
Successfully built d3268e345f3d
Creating project1_web_1
$ docker-compose -p project2 up -d
...
Successfully built d3268e345f3d
Creating project2_web_1
Unfortunately, this creating new images for each copy:
$ docker images
project1_web latest d3268e345f3d 2 hours ago 682 MB
project2_web latest d3268e345f3d 2 hours ago 682 MB
Question
Is there a way to use docker-compose to spin up multiple instances of a multi-container app by using a single image?
You can re-use your docker compose template by specifying the project name (which defaults to the directory name):
$ docker-compose --project-name inst1 up -d
Creating inst1_web_1
$ docker-compose --project-name inst2 up -d
Creating inst2_web_1
You could also scale up the container instances within a project:
$ docker-compose --project-name inst2 scale web=5
Creating and starting 2 ... done
Creating and starting 3 ... done
Creating and starting 4 ... done
Creating and starting 5 ... done
There should now be 6 containers running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e4ab4cebacf tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32772->8080/tcp inst2_web_2
ced61f9ac2db tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32773->8080/tcp inst2_web_5
efb1ef13147c tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32771->8080/tcp inst2_web_4
58e524da3473 tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32770->8080/tcp inst2_web_3
0f58c3c3b0ed tomcat:8.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:32769->8080/tcp inst2_web_1
377e3e5b03e4 tomcat:8.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:32768->8080/tcp inst1_web_1
If you want to reuse the image, you should build the image independent of the compose script.
run docker build -t somewebapp/web:latest
Then change your build section of docker-compose.yml to reference an image.

Resources