Docker Image and Container
Docker is like a magic box that lets you put your apps and everything they need inside a little package called a ‘container.’ These containers are like tiny, self-contained worlds where your app can live happily without bothering anything else on your computer.
Docker makes it super easy to share and run your apps without worrying about compatibility issues or messing up your computer’s setup.
Installing Docker
Install docker on ubuntu with this command
1
2
sudo snap refresh
sudo snap install docker
Creating Dockerfile
All you need to create a docker image is a Dockerfile, A Dockerfile is a text file used to define the instructions for building a Docker image. It serves as a recipe or set of step-by-step instructions that Docker uses to create a container image.
1
sudo nano Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Using base image of the latest ubuntu server
# # Docker requires you to have a base image as a base os to install your apps off it, here we'll just use the latest ubuntu release.
FROM ubuntu:latest
# Installing all the needed depedencies
# # Everything that you need to be installed on your docker container, specify the installation here
RUN apt update
RUN apt install sudo -y
RUN apt install openssh-server -y
RUN apt install python3 -y
RUN apt install python3-pip -y
RUN apt install git -y
RUN apt install nano -y
RUN apt install telnet -y
# Creating user helena:helena
RUN useradd -rm -d /home/helena -s /bin/bash -g root -G sudo -u 1000 helena
RUN echo 'helena:helena' | chpasswd
# Adding the 'helena' to the sudo group to grant sudo permissions
# # To access your container later on with ssh, you need to have a user. This additional command lets your user runs sudo command without requiring password.
RUN usermod -aG sudo helena
RUN echo 'helena ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
# starting the ssh service
RUN service ssh start
# changing user to helena
# # Creating directories and files is better done using standard user rather than root.
USER helena
# Cloning the GitHub repository into the container
WORKDIR /home/helena/
RUN git clone https://github.com/helenaferdy/Project1.git
# installing pip requirements that came with with the repository
RUN pip3 install -r Project1/xetc/requirements.txt
# exposing the ssh port
EXPOSE 22
# changing user to root
USER root
# creating service for the python app so it can be easily executed as a binary file from any directory
WORKDIR /home/helena/Project1/xetc
RUN cp run /usr/local/bin/
RUN cp update /usr/local/bin/
RUN chmod +x /usr/local/bin/run
RUN chmod +x /usr/local/bin/update
# adding ssh key
# # This server will need to connect to other machines using ssh, so having this key will help avoiding any key errors.
RUN mkdir /home/helena/.ssh/
RUN cp ssh_config.txt /home/helena/.ssh/config
# customizing log in message
# # Completely optional, but it's pretty cool to have a custom banner message when logging in to the container.
RUN chmod -x /etc/update-motd.d/*
RUN cp motd.txt /etc/update-motd.d/01-custom
RUN chmod +x /etc/update-motd.d/01-custom
# starting sshd
CMD ["/usr/sbin/sshd","-D"]
Building Docker Image
After the dockerfile is created, you can just start building the image
1
sudo docker build -t helena .
helena is the name of the docker image and “.” specifies where the Dockerfile is located
The docker image will start being built following the instructions given in the dockerfile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
helena@ubuntu2:~/docker$ sudo docker build -t helena .
Sending build context to Docker daemon 3.584kB
Step 1/31 : FROM ubuntu:latest
latest: Pulling from library/ubuntu
3153aa388d02: Pull complete
Digest: sha256:0bced47fffa3361afa981854fcabcd4577cd43cebbb808cea2b1f33a3dd7f508
Status: Downloaded newer image for ubuntu:latest
---> 5a81c4b8502e
Step 2/31 : RUN apt update
---> Running in 60f1869140bd
.
.
.
.
Step 31/31 : CMD ["/usr/sbin/sshd","-D"]
---> Running in 140a5037d01a
Removing intermediate container 140a5037d01a
---> 5473bd6c8f3e
Successfully built 5473bd6c8f3e
Successfully tagged helena:latest
After all the steps finish, you can see your image with this command
1
sudo docker images
1
2
REPOSITORY TAG IMAGE ID CREATED SIZE
helena latest 5473bd6c8f3e About a minute ago 1.33GB
Running the image as a docker container
To run the image as a container, run this
1
sudo docker run -d -p 9022:22 -t helena
-d: This is a flag that stands for “detached” mode to run the container in the background.
-p 9022:22: This is the port mapping option. It maps port 9022 on the host system to port 22 inside the container. This will allow you to ssh to the container on port 9022
-t: This flag assigns a pseudo-tty (terminal) to the container, allowing you to interact with the container’s shell.
Now the container is up and running, run this to view
1
sudo docker ps -a
1
2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71b3ffb11c40 helena "/usr/sbin/sshd -D" 4 seconds ago Up 2 seconds 0.0.0.0:9022->22/tcp, :::9022->22/tcp wonderful_franklin
ps: This sub-command stands for “process status” and is used to list containers.
-a: This flag stands for “all” and tells Docker to list all containers, including those that are currently running and those that have stopped (exited).
To access your container, just ssh to localhost on port 9022
1
ssh -p 9022 helena@localhost
Then you’ll be able to access the container shell while also be greeted by custom banner made earlier.
1
2
3
4
5
6
7
8
9
10
11
12
13
**************************************************************
Welcome to ProjectOne
**************************************************************
* type "run" to execute the application
* type "update" to update the application
* place your csv file inside the directory 'Project1/import/'
* all your output will be saved in 'Project1/out/'
**************************************************************
helena@71b3ffb11c40:~$
Additional usefule docker commands :
to stop a running container
- sudo docker stop [container-id]
to remove a stopeed container- sudo docker rm [container-id]
to remove an image- sudo docker rmi [image-id]
to remove all unused Docker resources, including stopped containers, dangling (unreferenced) images, unused networks, and dangling build cache- sudo docker system prune -a
Pushing the image onto Docker Hub
To easily share the image with other people, you can upload it to Docker Hub. Make sure to create an account first.
To login, run this command
1
sudo docker login
1
2
3
4
5
6
7
8
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: helenaferdy
Password:
WARNING! Your password will be stored unencrypted in /root/snap/docker/2893/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
Run this command to give the image a proper tag
1
sudo docker tag helena helenaferdy/project1:v1
Now if you list your images, you’ll see the newly tagged image
1
2
3
4
5
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
helenaferdy/project1 v1 5473bd6c8f3e About a minute ago 1.33GB
helena latest 5473bd6c8f3e About a minute ago 1.33GB
To upload it, run this
1
sudo docker push helenaferdy/project1:v1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
The push refers to repository [docker.io/helenaferdy/project1]
0a7aa6c4c2e1: Pushed
bde35d9065f8: Pushed
7a885719353e: Pushed
02173c9c9e42: Pushed
31085a65a19c: Pushed
3a0116aa3155: Pushed
e97c34e72a0c: Pushed
d4a52e8ae0aa: Pushed
5e5d5f388927: Pushed
c9303af71dc3: Pushing [=====================================> ] 578MB/764.1MB
51469af3f523: Pushed
11f79113dff8: Pushed
eb16500061fa: Pushed
8713df2134a0: Pushed
f2c4d808467c: Pushed
b4c1dfd4aa4d: Pushed
9abf2c356a15: Pushed
3cb9c57a28f6: Pushed
16b690872896: Pushed
781c116f1907: Pushed
9e958ca82432: Pushed
5f33815015a2: Pushed
b75c7318464c: Pushed
052c8ce84ced: Pushed
59c56aee1fb4: Layer already exists
After the push finishes, the image will be available on the docker hub site.
To pull the image onto other machine, simply run
1
sudo docker pull helenaferdy/project1:v1