Useful Docker Stuff
Installation:
sudo apt install docker.io
-- install dockersystemctl status docker
-- check statussudo systemctl enable docker
-- if not enabled then enable so it starts on bootsudo systemctl start docker
-- if not started then start servicesudo docker run hello-world
-- test to see if the default docker container workssudo usermod -aG docker $USER
-- add current user to docker group (so you don't need Sudo)- reboot machine and run
docker run hello-world
Running Containers:
docker images
-- check installed docker imagesdocker search ubuntu
-- search for the "ubuntu" docker imagedocker pull ubuntu
-- download the "ubuntu" docker imagedocker run ubuntu
-- create a container for "ubuntu" docker image (if a container has no jobs/automatic services then the container will exit immeditately)docker run nginx
-- creates a container for "nginx" docker image (this will run because this docker image automatically runs the nginx service - has a job). If the container has not been installed locally then it will be automatically downloaded.docker ps
-- lists docker containers currently running on the systemdocker ps -a
-- lists docker containers that have or are currently running on the system (all)docker run -it ubuntu /bin/bash
-- creates a "ubuntu" docker container and opens a bash terminal for you to interact with it (press<Ctrl-D>
to exit)- can get the same with
docker run -it ubuntu
(-it
= interactive terminal)
by default, containers don't save changes (i.e. app installs) as they are not stateful; when they are stopped they are deleted. If you want to keep changes then they need to be part of the docker image (the blueprint)
Making containers persistent:
docker run -it -d ubuntu
-- creates an interactive terminal for the "ubuntu" docker image and runs it in deamon mode (-d
), which runs it in the backgrounddocker ps
->docker attach <container_id>
-- attaches to the backgrounded docker image so you can interact with it (you don't have to type the whole container id, you can just type enough to distinguish it from the other containers)- whilst in a container press
<Ctrl-P> + <Ctrl-Q>
to background the container and keep it running
Accessing containerized apps:
A docker entrypoint command (
/docker-entrypoint.sh
) is a command which runs automatically when a container starts. I will keep running and not return you back to your shell when you start a container (i.e. nginx)
docker run -it -d -p 8080:80 nginx
-- runs the "nginx" container in daemon mode, creates an interactive terminal, and maps local port 8080 to port 80 on the container. You can then connect to local port 8080 to access port 80 on the container, or the IP address of the machine docker is running on and port 8080)docker run -it -d --restart unless-stopped -d 8080:80 nginx
-- same as before, but this time you can attach to the container, exit, and then the container will keep running. You have to explicitly stop the container to kill it. This is useful if you have a container which needs to be avaliable as it solves a purpose (i.e. serving a website).
Creating docker image:
- try to find a container which is as close to the container you want to create:
docker run -it ubuntu
->apt update && apt upgrade
- install packages you want:
apt install apache2 nano
- check status of installed packages/services and get them running:
/etc/init.d/apache2 status
->/etc/init.d/apache2 start
- disconnect from container and keep the changes made:
<Ctrl-P> + <Ctrl-Q>
- get the container's id and turn container into a re-usable image:
docker ps
docker commit --change='ENTRYPOINT ["apachectl", "-DFOREGROUND"] <container_id> lltv/apache-txt:1.0
-- creates a new container image called "lltv/apache-txt:1.0" with an entrypoint that starts apache
- check image was created with
docker images
- stop the current container with
docker stop <container_id>
- run the new container image with
docker run -d -it -p 8080:80 lltv/apache-txt:1.0
to remove a docker image run
docker rm <container_id>
and `docker
Creating docker image from a docker file:
- create a docker file with
nano Dockerfile
:
FROM ubuntu
MAINTAINER dev <[email protected]>
# skip prompts
ARG DEBIAN_FRONTEND=noninteractive
# update packages
RUN apt update; apt dist-upgrade -y
# install packages
RUN apt install -y apache2 nano
# set entrypoint
ENTRYPOINT apache2ctl -D FOREGROUND
- build the docker image form the file:
docker build -t lltv/apache-test:1.1 .
-- build "lltv/apache-test:1.1" from the docker file in the current working directory (.
)- you need to make sure the docker file automatically builds the image without asking for any prompts (i.e.
ARG DEBIAN_FRONTEND=noninteractive
and-y
apt option)
Removing docker images:
- remove docker containers associated with image:
docker ps -a
docker rm <container_id>
- remove docker image:
docker images
docker rmi <image_id>