Docker reminder
Some useful information
Docker cli
docker exec -it <container_name> /bin/bash # List active containers docker ps # ou docker container ls # List active containers and also stopped containers docker ps -a # Or docker container ls -a # list images docker images docker image rm <image_id> # force remove a docker image even if this image is used by a stopped container => -f option to force docker image rm -f <image_id> # build an image from a dockerfile named Dockerfile in the current directory. The image is tagged as hello:v0.1 docker image build -t hello:v0.1 . # The equivalent with the dockerfile name docker image build -f Dockerfile hello:v0.1 . # Same without using cache docker image build --no-cache -t hello:v0.1 .--no-cache # Start container with name mycontainername , -d for detached process, -p for mapping ports, and at the end we specify the tag docker run --name mycontainername -d -p 7777:7777 hello:v0.1 # Inspect an image docker inspect <image_id> # Create an image of our container # but the best way to create an image is a dockerfile, # dockerfile permits source versioning and also is an easy way to modify an image docker container commit <container_id> # Tag the image docker tag image <image_id> <our_tag> # Warning: delete all stopped containers + all networks not used by at least one container + all dangling images + all build cache docker system prune --volumes --all # By default, volumes are not removed to prevent important data from being deleted if there is currently no container using the volume. # Use the --volumesflag when running the command to prune volumes as well # Remove all stopped containers + all networks not used by at least one container + all volumes not used by at least one container + all images without at least one container associated to them + all build cache docker system prune --all --force --volumes
Dockerfile
ENTRYPOINT
The entry point defines the executable which is used to exectute the commands give with the COMMAND directive.
Default ENTRYPOINT is /bin/sh -c
ENTRYPOINT is optional
ENTRYPOINT ["/usr/bin/java"] CMD ["-version"] # ==> will result into /usr/bin/java -version (which is useless, it's just an example)
# juste /bin/bash instead of default /bin/sh ENTRYPOINT ["/bin/bash", "-c"]
CMD
CMD can be interpreted as CMD [“executable”, “param1”, “param2”]
Only one CMD by dockerfile, it defines the default “command” to execute when starting a container.
# Then you can't have this => CMD ["echo 'coucou';echo 'coucou2'", "/usr/bin/sshd"]
There it will be interpreted as /bin/sh -c echo ‘coucou’;echo ‘coucou2’ /usr/bin/sshd… ==> ERROR
To do this kind of things:
CMD ["/bin/sh", "-c", "echo 'coucou';echo 'coucou2';/usr/bin/sshd"]
IMPORTANT: if the docker container runs with a command argument (passed from cli or from docker-compose.yml) then CMD will be ignored
Enable systemd inside your container
It can be interesting to install and enable systemd in some cases, for instance when you wan to create a test environment to mimic real servers.
# installation et preparation de systemd RUN yum -y install systemd; yum clean all; \ (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \ rm -f /lib/systemd/system/multi-user.target.wants/*;\ rm -f /etc/systemd/system/*.wants/*;\ rm -f /lib/systemd/system/local-fs.target.wants/*; \ rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ rm -f /lib/systemd/system/basic.target.wants/*;\ rm -f /lib/systemd/system/anaconda.target.wants/*; # Put this line at the end of the Dockerfile # Init systemd CMD ["/usr/sbin/init"]
You can then use systemctl.
If you have also installed and enabled sshd in some cases you can get the error System is booting up. See pam_nologin(8). As explained in this link you can overcome this error by removing the following file:
rm -f /var/run/nologin # sometimes need to remove /run/nologin
Docker compose
docker-compose up docker-compose kill # ⇒ send SIGKILL docker-compose stop # ⇒ only stop containers but don’t remove them docker-compose down # For a clean start: docker-compose rm -f docker-compose up --force-recreate docker-compose up --build --force-recreate
docker-compose.yml
command: /data/dockerscripts/startup.sh # equivalent to CMD [ "/data/dockerscripts/startup.sh" ] BUT CAREFUL it will replace the CMD of your container (CMD from the dockerfile used to build the image of the container)