Can not start Docker container - docker

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

Related

Updating a docker container from image; leaves old images on server

My process for updating a docker image to production (a docker swarm) is as follows:
On dev environment:
docker-compose build
docker push myrepo/name
Then on the prod server, which is a docker swarm:
docker pull myrepo/name
docker service update --image myrepo/name --with-registry-auth containername
This works perfectly; the swarm is updated with the latest image.
However, it always leaves the old image on the live servers and I'm left with something like this:
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myrepo/name latest abcdef 14 minutes ago 1.15GB
myrepo/name <none> bcdefg 4 days ago 1.22GB
myrepo/name <none> cdefgh 6 days ago 1.22GB
Which, over time results in a heap of disk space being unnecessarily used.
I've read that docker system prune is not safe to run on production especially in a swarm.
So, I am having to regularly, manually remove old images e.g.
docker image rm bcdefg cdefgh
Am I missing a step in my update process, or is it 'normal' that old images are left over to be manually removed?
Thanks in advance
since you are using docker swarm and probably multi node setup you could deploy a global service which would do the cleanup for you. We are using Bret Fisher's approach on it:
version: '3.9'
services:
image-prune:
image: internal-image-registry.org/proxy-cache/library/docker:20.10
command: sh -c "while true; do docker image prune -af --filter \"until=4h\"; sleep 14400; done"
networks:
- bridge
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
mode: global
labels:
- "env=devops"
- "application=cleanup-image-prune"
networks:
bridge:
external: true
name: bridge
When adding new hosts it gets deployed automatically on it with our own base docker image and then does the cleanup job for us.
We are still missing some time to inspect newer docker service types which are scheduled on their own. It would probably be wise to move cleanup jobs to the global service replicated jobs provided by docker instead of an infinite loop in a script. It just works for us so we did not make it high priority enough to swap over to it. More info on the replicated jobs

Docker Run Options in a Docker Compose File?

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.

How to run "docker-compose up" pointing to a different image ID

Apoligies if this question is dumb or naive... we are still learning docker. We are running Airflow in docker. Here are the docker images on our GCP compute engine:
ubuntu#our-airflow:~/airflow-dir$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
our-airflow_webserver latest aaaaaaaaaaaa 17 minutes ago 968MB
<none> <none> bbbbbbbbbbbb 22 minutes ago 2.13GB
apache/airflow 2.1.4 cccccccccccc 5 weeks ago 968MB
<none> <none> dddddddddddd 2 months ago 2.01GB
python 3.7-slim-buster eeeeeeeeeeee 17 months ago 155MB
postgres 9.6 ffffffffffff 17 months ago 200MB
ubuntu#our-airflow:~/airflow-dir$
dddddddddddd was the image that used to run when we ran docker-compose up from the command line. However, we were testing a new Dockerfile, and built the new image aaaaaaaaaaaa with the tag our-airflow_webserver. dddddddddddd used to have this tag, but it was changed to <none> when we built aaaaaaaaaaaa.
We'd like to run docker-compose up dddddddddddd, however this does not work. We get the error ERROR: No such service: dddddddddddd. How can we create a container using the image dddddddddddd with docker-compose up? Is this possible?
Edit: If I simply run docker run dddddddddddd, I do not get the desired output. I think this is because our docker-compose file is launching all of the requisite services we need for airflow (webserver, scheduler, metadata db).
Edit2: Here's the seemingly relevant webserver part of our docker-compose file:
webserver:
# image:
build:
dockerfile: Dockerfile.Self
context: .
can we simply uncomment image, and set it to image: dddddddddddd and then comment out the build part?
can we simply uncomment image, and set it to image: dddddddddddd
Yes, you can. If you want to start the service with another image you must change the docker-compose.yml file.
and then comment out the build part?
You don't need to comment the build part. The build just takes action when the image specified is not found or the --build option is passed as argument.
If you want to ensure that the image is not gonna be build, just pass the argument --no-build to docker-compose up command. This will avoid to build the image even if it's missing.
Check the docker-compose up docs for further information.

Running a ubuntu container in background using docker compose

I am able to run a docker container using following docker command:
docker run -it ubuntu /bin/bash
Now I am trying to do it by using docker-compose:
version: "3"
services:
ubuntu:
container_name: ubuntu
image: ubuntu
restart: on-failure
command: "/bin/bash"
Now when I do :
docker-compose up -d
Can see docker container starting and exiting immediately.
I tried looking at the logs :
docker logs b8 //b8 is container id
But there are no error logs.
How do I keep ubuntu container running in background using docker.
( I am using docker on windows , linux version)
This is normal.
You are starting an ubuntu container with bash as the command (thus the root process). The thing is to keep bash alive you need to attach it with a terminal. This is why when you want to get a bash in a container, you're using -ti with your command :
docker container exec -ti [my_container_id] bash
So if you want to keep your ubuntu container alive and don't want to attach it to a terminal, you'll have to use a process that will stay alive for as long as you want.
Below is an example with sleep infinity as your main process
version: "3"
services:
ubuntu:
container_name: ubuntu
image: ubuntu
restart: on-failure
command: ["sleep","infinity"]
With this example, you container will stay running indefinitely.

What is the difference between docker run -p and ports in docker-compose.yml?

I would like to use a standard way of running my docker containers. I have have been keeping a docker_run.sh file, but docker-compose.yml looks like a better choice. This seems to work great until I try to access my website running in the container. The ports don't seem to be set up correctly.
Using the following docker_run.sh, I can access the website at localhost. I expected the following docker-compose.yml file to have the same results when I use the docker-compose run web command.
docker_run.sh
docker build -t web .
docker run -it -v /home/<user>/git/www:/var/www -p 80:80/tcp -p 443:443/tcp -p 3316:3306/tcp web
docker-compose.yml
version: '3'
services:
web:
image: web
build: .
ports:
- "80:80"
- "443:443"
- "3316:3306"
volumes:
- "../www:/var/www"
Further analysis
The ports are reported as the same in docker ps and docker-compose ps. Note: these were not up at the same time.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<id> web "/usr/local/scripts/…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:3307->3306/tcp <name>
$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------------------
web /usr/local/scripts/start_s ... Up 0.0.0.0:3316->3306/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
What am I missing?
As #richyen suggests in a comment, you want docker-compose up instead of docker-compose run.
docker-compose run...
Runs a one-time command against a service.
That is, it's intended to run something like a debugging shell or a migration script, in the overall environment specified by the docker-compose.yml file, but not the standard command specified in the Dockerfile (or the override in the YAML file).
Critically to your question,
...docker-compose run [...] does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag.
Beyond that, the docker run command you show and the docker-compose.yml file should be essentially equivalent.
You don't run docker-compose.yamls the same way that you would run a local docker image that you have either installed or created on your machine. docker-compose files are typically launched running the command docker-compose up -d to run in detached mode. Then when you run docker ps you should see it running. You can also run docker-compose ps as you did above.

Resources