Skip to content

Latest commit

 

History

History
138 lines (112 loc) · 6.93 KB

docker_notes.md

File metadata and controls

138 lines (112 loc) · 6.93 KB

DOCKER NOTES

Installing Docker, first checks and configs

sudo apt install docker.io to install Docker
systemctl status docker to check if Docker is running.

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-04-06 20:35:19 BST; 3min 46s ago

Docker is running and enabled.
If it is disabled: sudo systemctl enable docker to enable it.
sudo systemctl start docker to run Docker if it is inactive.

Run sudo docker run hello-world to check if Docker is functional.
Docker will pull the container and display a welcome message.

Add your user to the docker group if you don't want to use sudo to run it.
sudo usermod -aG docker pi is the command (exchange pi with your username).
That change will take effect after the next login.

Docker commands

docker images shows you all installed Docker repositories.
docker search debian will show you available debian docker repos.
docker pull arm32v7/debian will download that image to your local system.
You can download and run the image in one command: docker run arm32v7/debian
The same command will run a container from the prior downloaded image.
NOTE: Containers which have nothing to do will stop running automatically.
To determine which docker containers are currently running, use: docker ps.
docker ps -a will show you running containers + a history of containers that recently ran.
So while docker run arm32v7/debian will straight exit because it has nothing to do … docker run -it arm32v7/debian /bin/bash will give you a bash prompt.
You can enter any command as on an usual arm32v7/debian bash shell – like installing Vim:

root@d53eba57a5a1:/# apt update; apt install vim

CTRL + d to exit the container.
NOTE: If you exit and re-enter the container, you'll notice that Vim ain't installed.
Docker doesn't save any changes made to a container by default!
If you want to make a permanent change to a container, you have to change the image.
A container is build from the image when you use docker run, so what ain't in the image won't be build.
docker run -it arm32v7/debian without the /bin/bash addition brings you to the promt as well.
bash is the default shell of the debian image and called through the -it option. If you enter docker run -it -d arm32v7/debian you'll get some string like this as result:

1dd819d65581b45611a197d211f2314eee9b8529cfc1ca807fe7a4c148c0cb95

If you run docker ps now, you'll see, that the container is still running in the background:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS       
1dd819d65581        arm32v7/debian      "bash"              7 minutes ago       Up 7 minutes

So let's walk through the docker run -it -d arm32v7/debian command:
-it is for interactive terminal – that will open the default shell.
-d the -d option puts the container into deamon mode, which means it is running in the background.
To get back into that container, use docker attach 1dd819d65581.
Docker allows you to type those letters from the container id which deffer from those of the other running containers.

If you detach from the container with CTRL + d or exit, the container will stop and all the changes made to the container will be lost.
To exit the container while keeping it running in the background, hold CTRL and type p, then q.
You'll get such a message:

root@1dd819d65581:/# read escape sequence

The container will now remain in the background.
If you re-attach to it, you'll realize, your changes have been kept.

Interacting with containers

Installing a web-server and mapping a port

docker run -it -d -8080:80 nginx will run an container of an nginx image, with …

  • -it an interactive shell
  • d daemon mode
  • port 80 (on which nginx runs by default) enabled
  • port 8080 from the host system mapped to that port

Get the ip address of the host with ip addr show or short ip a.
Enter your browser, type in that address plus the specified port, in my case http://192.168.178.41:8080/
… and you'll hopefully see the ngnix welcome page.
Use docker run -it -d --restart unless-stopped -p 8080:80 nginx if you want to keep ngnix alive until stopped.

Docker start

You can restart a container you exited or stopped by command with docker start 1dd819d65581.

Docker commit

docker commit 1dd819d65581 cranky_hamilton:1.0 will create an image …

  • from container 1dd819d65581
  • with name cranky_hamilton
  • in version 1.0

This command will create a new image from the container including all the changes made to the original image.

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cranky_hamilton     1.0                 ed24b7782983        19 hours ago        375MB

docker run -d -it --restart unless-stopped cranky_hamilton:1.0 will run a container from that image.

You can specify entry point and other options directly in the commit command. Example:
docker commit --change='ENTRYPOINT ["apachctl", "-DFOREGROUND"]' 1dd819d65581 cranky_hamilton:1.1

Docker files

Ressources: wiki.learnlinux.tv
Optional: Create docker file directory mkdir dockerfiles and enter it cd dockerfiles
vim Dockerfile to create a new file.

FROM arm32v7/ubuntu
MAINTAINER <[email protected]>

# skip prompts
ARG DEBIAN_FRONTEND=noninteractive 

# Update packages
RUN apt update; apt upgrade -y

# Install packages
RUN apt install -y vim tmux htop neofetch curl

# Curl dotfiles
RUN curl https://raw.githubusercontent.com/HeikoKramer/linux/main/dotfiles/remote/.bashrc > .bashrc; curl https://raw.githubusercontent.com/HeikoKramer/linux/main/dotfiles/remote/.vimrc > .vimrc; curl https://raw.githubusercontent.com/HeikoKramer/linux/main/dotfiles/remote/.bash_aliases > .bash_aliases; curl https://raw.githubusercontent.com/HeikoKramer/linux/main/dotfiles/remote/.bash_remote > .bash_remote; curl https://raw.githubusercontent.com/HeikoKramer/linux/main/dotfiles/tmux.conf > .tmux.conf;

Remove image

docker image rm arm32v7/debian to remove the arm32v7/debian image. If you get a conflict error message, you should stop containers running the image. docker stop 50a4bb400ca7 will stop the container with that id.
If the image still doesn't get deleted or you don't care about running containers,
run docker image rm -f arm32v7/debian to force the deletion of the image.