Is it safe to remove Docker containers listed with `docker ps -f status=created`? - unix

I've already seen posts showing how to remove exited containers listed with docker ps -q -f status=exited, but I also want to clean up 'created' but not 'running' containers. Is it safe to remove containers with the 'created' status, or is there a downside to this?

Docker containers with created status are containers which are created from the images, but never started. Removing them has no impact as you would not have run any process within the container and causing a change in the state of the created container, in the later case requires to be committed. This is generally done to speed up starting the container and making sure all the configuration is kept ready.
Refer Docker Docs
The docker create command creates a writeable container layer over the
specified image and prepares it for running the specified command. The
container ID is then printed to STDOUT. This is similar to docker run
-d except the container is never started. You can then use the docker start command to start the container at any point.
This is useful when you want to set up a container configuration ahead
of time so that it is ready to start when you need it. The initial
status of the new container is created.

There is two possibility for a container to be in the created status :
As explained by #askb docker container created from the image using docker create command will end up in the create command
A docker container created by the run command but not able to start. Multiple causes here but the easiestone is a docker container with a port mapping to an already bind ones
To answer the question, in both cases, removing them is safe.
A way to reproduce the docker container in a created state via the run command is :
docker pull loicmathieu/vsftpd
docker run -p 621:21 -d loicmathieu/vsftpd ftp
docker run -p 621:21 -d loicmathieu/vsftpd ftp
Then docker ps -a will give you something like
CONTAINER ID IMAGE COMMAND CREATED STATUS
e60dcd51e4e2 loicmathieu/vsftpd "/start.sh ftp" 6 seconds ago Created
7041c77cad53 loicmathieu/vsftpd "/start.sh ftp" 16 seconds ago Up 15 seconds

Related

How do I enter my container if dokku enter <app> does nothing?

I have a simple dokku app using herokuish buildpack-php with a procfile: web: vendor/bin/heroku-php-apache2 public/
If I try to enter the app with dokku enter <appname> nothing happens, and I am simply returned to my host shell.
I can run dokku run <appname> bash and get a shell, but is, as far as I understand from the documentation placing me in a new container and not in the existing/running one I need access to:
The run command can be used to run a one-off process for a specific command. This will start a new container and run the desired command within that container.
How can I fix this so I can enter my running container?
If you are in the local directory for your app, doing the following will take you into the running container for it:
dokku enter web.1
I've done this a few times today!
As a workaround, you can enter your container using docker. For doing so, run docker ps and note the container ID. Afterwards, run docker exec -it container_id /bin/bash.

how to share data between docker container and host

I'm working on a read the docs documentation where I use docker. To customize it, I d like to share the css folder between the container and host, in order to avoid building always a new image to see the changes. The goal is, that I can just refresh the browser and see the changes.
I tried something like this, but it doesn't work:
docker run -v ~/docs/source/_static/css:/docs/source/_static/css -p 80:80 -it my-docu:latest
What is wrong in this command?
The path of the folder I'd like to share is:
Documents/my-documentation/docs/source/_static/css
Thanks for your help!
I'm guessing that the ~ does not resolve correctly. The tilde character ("~") refers to the home directory of your user; usually something like /home/your_username.
In your case, it sounds like your document isn't in this directory anyway.
Try:
docker run -v Documents/my-documentation/docs/source/_static/css:/docs/source/_static/css -p 80:80 -it my-docu:latest
I have no mac to test with, but I suspect the command should be as below (Documents is a subfolder to inside your home directory denoted by ~)
docker run -v ~/Documents/my-documentation/docs/source/_static/css:/docs/source/_static/css -p 80:80 -it my-docu:latest
In your OP you mount the host folder ~/docs/source/_static/css, which does not make sense if your files are in Documents/my-documentation/docs/source/_static/css as that would correspond to ~/Documents/my-documentation/docs/source/_static/css
Keep in mind that Docker is still running inside a VM on Mac, so you will need to give a host path that is valid on that VM
What you can do to get a better view of the situation is to start an interactive container where you mount the root file system of the host vm root into /mnt/vm-root. That way you can see what paths are available to mount and how they should be formatted when you pass them using the -v flag to the docker run command
docker run --rm -it -w /mnt/vm-root -v /:/mnt/vm-root ubuntu:latest bash

Docker WordPress image does not persist wp-content when creating new docker images

let me clarify the situation:
Run wordpress docker container with:
docker run --name wp -d -p 80:80 wordpress
Login to a running container using bash:
docker exec -it wp /bin/bash
Create 2 dummy files:
One in root:
touch /xxx
One in wp-content/themes
touch /var/www/html/wp-content/themes/xxx
Create a new wordpress image:
docker commit wp new_wp
Kill the original container:
docker kill wp
Run new docker image:
docker run --name new_wp -d -p 80:80 new_wp
Inspect dummy files created in step 3:
Dummy file in root exists
Dummy file in wp-content/themes no longer exists!!!
Questions:
Can anyone explain such a bizare behaviour in step 7?
What am I supposed to do to persist wp-content data?
P.S. I am deploying to AWS ECS Fargate instances therefore using volumes is not very practical for me. Ideally - I would love to have everything under one image without files disappearing from wp-content directory.
Thank you very much for your answers.
The docker image for wordpress includes a VOLUME statement:
VOLUME /var/www/html
This forces a volume to be created on any resulting containers even if you do not specify one in your docker run command. Without a specification, you will get an anonymous volume with a long unique id that can be seen in docker volume ls.
The docker commit command (which I strongly recommend against using in any workflow that you want repeatability) only captures changes to the container filesystem (you can see these with docker container diff). The changes to the volume are not part of the container filesystem, and therefore will not be included in this commit.
To persist data, you should be defining and using a volume, e.g.:
docker run --name wp -v wpdata:/var/www/html -d -p 80:80 wordpress
Docket is inherently non-persistent.
If you want to leverage docker for WP I highly recommend offloading image asset management to S3 and Cloudfront.

Docker run start services

I need nginx-openresty and redis in single docker container. I have written docker file its working fine. But thing i need to start my redis service after login into the docker bash to automate this I have written .sh file which contains instrutions like start and stop of redis server and nginx. ENTRYPOINT ["./startup.sh"]
and .sh file is
cd /etc/redis-installation/utils
echo -n | ./install_server.sh
service redis_6379 stop
cd /
cp ./dump.rdb /var/lib/redis/6379/
service redis_6379 start
openresty
My problem is that docker container start and exist when shell execution completed. How can stay the container keep running with nginx and redis in running state.
Try using docker-compose with a link between your app container and your redis container. I suggest using the official redis container

Docker shows inconsistent behaviour when creating container from image

I am developing a web application which depends on a moodle system, as it uses moodles webservices. For my automated tests, I wanted to use docker to provide a preconfigured moodle-application on all my machines. Therefore I created a docker image, which I import from a .tar.gz file.
However, creating a new container-instance from this image behaves inconsistently. Sometimes the container boots up correctly and everything works fine. However, sometimes the container starts but the moodle-website is not reachable. If I connect my bash to the container using docker exec -it <container> bash I see that apache is running. The error logs do not show any entries which might be related to this issue.
If I kill the container instance and boot it up again, everything works as expected (sometimes this step has to be repeated multiple times). Do you have any idea what could be the reason for this strange behaviour? Anyone experiencing similar issues?
Docker is running on Ubuntu 14:04. The problem appears on several machines. The script which imports the image and starts the container looks like this:
#!/usr/bin/env bash
docker rm -f moodle
docker load < my-moodle.tar.gz
docker run -d -p 8080:80 -p 8443:443 -p 3306:3306 --name moodle moodle-image
Thanks in advance!
Successful container startup depends on your container entrypoint and external resources (if the entrypoint has external dependencies). What is the entrypoint? Does it depend on external resources?

Resources