03 AG Create & Manage Docker Ed28
03 AG Create & Manage Docker Ed28
[email protected] 1
Contents
1 Introduction .........................................................................................................................................................4
2 Documentation ....................................................................................................................................................5
2.1 Linux Commands and VIM Commands ................................................................................................................ 5
3 Pre-Requisite .......................................................................................................................................................6
4 Docker installation steps on Ubuntu 20.04 server .................................................................................7
5 Working with Container................................................................................................................................ 10
5.1.1 Exposing the container port ...........................................................................................................................................11
3. 4 Working with Docker Images .............................................................................................................. 13
3. 5 Docker Default Bridge Networking.................................................................................................... 20
3. 6 Creating Custom Bridge Network ....................................................................................................... 26
3. 7 Docker Host Network.............................................................................................................................. 29
3. 8 Docker Storage – Host Path Mounting .............................................................................................. 31
3. 9 Docker Volume .......................................................................................................................................... 35
3. 10 Docker Private Registry ......................................................................................................................... 37
3. 11 Working with Dockerfile ....................................................................................................................... 39
5.2 ARG in Dockerfile ...................................................................................................................................................... 43
5.3 Using ARG variables ................................................................................................................................................. 43
5.4 Docker Volume .......................................................................................................................................................... 44
5.5 USER in Dockerfile .................................................................................................................................................... 44
3. 12 Docker Compose ....................................................................................................................................... 46
5.6 Compose V2 and the new docker compose command................................................................................. 46
5.7 Working with Application Stack .......................................................................................................................... 46
3. 13 Configuring External DNS, Logging and Storage Driver .............................................................. 53
3. 14 Multi-stage Dockerfile ............................................................................................................................ 56
3. 15 Multi-stage Dockerfile Another Example......................................................................................... 59
3. 16 Troubleshooting ....................................................................................................................................... 61
5.8 Getting error while restarting docker service after configuring external dns, logging & storage
driver......................................................................................................................................................................................... 61
5.9 Issue: Access Denied Error Lab-6 ........................................................................................................................ 62
5.10 Can’t pull Apache2 Image ................................................................................................................................... 63
5.11 Getting Error While Build and run the application with docker-compose Lab-14 ....................... 64
5.12 Unable to Docker Login or Create a new Docker ID ................................................................................. 68
5.13 Apache2 page not showing ............................................................................................................................... 68
5.14 http://hostip:5000 -> This site can’t be reached ...................................................................................... 69
5.15 Web container is in exit state ........................................................................................................................... 69
5.16 Runtime Error: invalid memory address when doing docker login ................................................... 70
3. 17 Extra Docker Question ........................................................................................................................... 72
3. 18 Summary ..................................................................................................................................................... 73
[email protected] 2
[email protected] 3
1 INTRODUCTION
Docker is a platform for developers and sysadmins to build, run, and share applications
with containers. The use of containers to deploy applications is called containerization.
Containers are not new, but their use for easily deploying applications is.
[email protected] 4
2 DOCUMENTATION
1. Docker Container
https://docs.docker.com/engine/reference/commandline/container/
2. docker container attach
https://docs.docker.com/engine/reference/commandline/container_attach/
3. docker container create
https://docs.docker.com/engine/reference/commandline/container_create/
4. Docker Image
https://docs.docker.com/engine/reference/commandline/images/
5. Docker File Refrence
https://docs.docker.com/engine/reference/builder/
[email protected] 5
3 PRE-REQUISITE
Ensure that you have completed following two activity guides (or you have an Ubuntu
Server)
• Create account (Trial or Paid) on Azure Cloud.
Note: Follow Activity Guide
Register_For_Azure_Cloud_Account_Accessing_Console_ed** from portal
• Installed Ubuntu Server (configure at least 2 vCPU) Note: Follow Activity Guide
Create_&_Connect_to_Ubuntu_Server_ed** from portal
• Linux for beginners’ course to make yourself familiar with vi editor and basic Linux
commands. If you are new to Linux, then ask for the FREE Bonus course.
[email protected] 6
4 DOCKER INSTALLATION STEPS ON UBUNTU 20.04 SERVER
1. [Optional] Only if docker was already installed on this host and you want to configure it again
Uninstall Old Versions of Docker.
(Older versions of Docker were called docker, docker.io, or docker-engine. If these are
installed, uninstall them)
$ sudo apt-get remove docker docker-engine docker.io containerd runc
2. Update the apt package
$ sudo apt-get update
(Note: Copy and paste the command on a notepad first then execute it.)
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o
/etc/apt/keyrings/docker.gpg
5. Use the following command to set up the stable repository.
(Note: The below is a single command, please Copy and paste the command on a notepad
first then execute it.)
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg]
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Note: Above command is in single line like below (first copy paste in notepad and then from
notepad paste on Linux terminal)
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg]
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
[email protected] 7
6. Update apt package again
12. Logout and log back in and check if user has group docker assigned
[email protected] 8
Note: If you don’t logout and log back in you may get error like below at later stage
[email protected] 9
5 WORKING WITH CONTAINER
1. Creating first Container
$ docker run -it ubuntu bash
Or
Note: If you don’t logout and log back in you may get error like below at later stage
$ docker container ls
[email protected] 10
5. List all Containers
$ docker ps -a
$ docker container ls -a
To access a container we first need to expose it’s port. We can use the options “-p” and “-P”.
‘-p’ - Publish a container's port(s) to the host
‘-P’ - Publish all exposed ports to random ports
[email protected] 11
3. Now we can access the app running inside the container from browser using the Public ip of
the host machine and the port like below-
http://<PublicIP>:8080
4. Deploy an nginx container by exposing its port to any random port using ‘-P’
$ docker run --name web2 -dit -P nginx
It has exposed to a random port i.e. 49153, Now we can access it using it.
[email protected] 12
3. 4 WORKING WITH DOCKER IMAGES
[email protected] 13
6. Commit Container to create Container Image
$ docker ps
$ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
$ docker commit bc35381ed053 ubuntu:new
$ docker container ls
[email protected] 14
9. Export Container as tar file (Persisting Container)
$ docker export bc3538 > ubuntu.tar
$ ls -lrt
[email protected] 15
$ docker save -o mynginx1.tar nginx
$ ls -lrt
[email protected] 16
15. Tag and Push image to Docker Hub
a) Login at the command line terminal with your Docker ID created in above step to push and
pull images from Docker Hub
$ docker login
b) We will tag the image with the registry name to push it to our named registry
So the format of tagging is:
[email protected] 17
docker tag SOURCE_NAME:TAG TARGET_NAME:TAG
TAGRET_NAME is above command should be of format:
Registry/repository:tagname
[email protected] 18
[email protected] 19
3. 5 DOCKER DEFAULT BRIDGE NETWORKING
[email protected] 20
$ docker info
[email protected] 21
5. Install brctl tools
$ sudo apt-get install bridge-utils
$ ip a
[email protected] 22
Inspect the Bridge network
docker0 bridge will have 1 new interfaces connected
$ brctl show
[email protected] 23
$ brctl show
[email protected] 24
11. Check if you are able to reach the other container on the same network
$ ping 172.17.0.3
12. Try to ping google.com and check internet connectivity inside the Container
$ ping google.com
[email protected] 25
3. 6 CREATING CUSTOM BRIDGE NETWORK
[email protected] 26
5. Connect to Container Cont1 and install iputils-ping
$ docker exec -it Cont1 bash
[email protected] 27
7. Check internet connectivity
root@9227d60ab1d3:/# ping google.com
[email protected] 28
3. 7 DOCKER HOST NETWORK
Note: To access nginx please use your azure machine host IP address. Also all port should be
open. To Open ports please refers to the Create_&_Connect_to_Ubuntu_Server_ed**
3. Open web browser of docker host and verify is the server is serving Nginx
[email protected] 29
4. Verify which process is bound to port 80, using the netstat command.
$ sudo netstat -tulpn | grep :80
[email protected] 30
3. 8 DOCKER STORAGE – HOST PATH MOUNTING
[email protected] 31
b. Move Your Curser using arrow keys go to the line number 198
c. Click on ESC button and Exit Insert mode and type :wq! to save and exit file
[email protected] 32
Save and quit from the file. Exit from the container without stopping it.
(Ctrl p+q)
5. Create container named "volsharing2" and mount the same host path to container
$ docker run -it -p 81:80 -v /home/vol-share:/var/www/html --name volsharing2 ubuntu
/bin/bash
Note: To access Apache please use your azure machine host IP address. Also all port should be
open. To Open ports please refers to the Create_&_Connect_to_Ubuntu_Server_ed**
[email protected] 33
[email protected] 34
3. 9 DOCKER VOLUME
4. Create container named "busybox-cont" and mount docker volume "volume1" @ /data in the
container
$ docker run -it -v volume1:/data --name busybox-cont busybox:latest
[email protected] 35
6. Quit from the "busybox-cont" container without exitting it
Press below keys in sequence Ctrl p + ctrl q
Enter Ctrl p + q, to exit from the container
7. Create container named "other-container" and mount docker volume "volume1" @ /data in the
container
$ docker run -it -v volume1:/data --name other-container busybox:latest
8. Look for the vol.txt file in "other-container" @ /data
/ # cd /data
/data # cat vol.txt
[email protected] 36
3. 10 DOCKER PRIVATE REGISTRY
[email protected] 37
$ docker tag jenkins:latest localhost:5000/jenkins:latest
[email protected] 38
3. 11 WORKING WITH DOCKERFILE
$ vim Dockerfile
Note: In Lab 10 Docker Storage - host path mounting, steps available for how to Edit/Save Files.
2. Add the following code to the dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
COPY index.nginx-debian.html /var/www/html
EXPOSE 80
COPY [“./start.sh”, ”/root/start.sh”]
ENTRYPOINT /root/start.sh
Note: when you copy this file from here Please retype the [“ ”] again manually otherwise in
later steps you will get the below error.
When you copy this from here the double quotes need to type again in file.
[email protected] 39
3. Create sample index.html file in the same directory
$ vim index.nginx-debian.html
5. Create script start.sh file in the same directory. Add the below content to start nginx daemon
on start. Also change the script file permission to executable.
$ vim start.sh
[email protected] 40
$ chmod 777 start.sh
6. Execute the Dockerfile (note that there is a space between build and .)
$ docker build .
$ docker images
[email protected] 41
$ docker run -d -p 82:80 nginxbuilt
Note: To access nginx please use your azure machine host IP address. Also all port should be
open. To Open ports please refers to the Create_&_Connect_to_Ubuntu_Server_ed**
11. Login to Docker Host web browser at port 82 and check if Nginx website is served
[email protected] 42
5.2 ARG in Dockerfile
1. The ARG instruction defines a variable that users can pass at build-time to the builder with the
docker build command using the --build-arg <varname>=<value> flag. If a user specifies a build
argument that was not defined in the Dockerfile, the build outputs a warning.
FROM busybox
USER ${user:-some_user}
ARG user
USER $user
A user builds this file by calling:
$ docker build --build-arg user=what_user .
The USER at line 2 evaluates to some_user as the user variable is defined on the subsequent line
3. The USER at line 4 evaluates to what_user as user is defined and the what_user value was
passed on the command line.
[email protected] 44
Check user and groups
$ docker run -it 6c2f395762ba bash
[email protected] 45
3. 12 DOCKER COMPOSE
Note: Docker Compose is now part of the Docker CLI it can be installed via a convenience script
with Docker Engine and the CLI. We have already installed it while installing docker.
[email protected] 46
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
Note: file indenting python is very sensitive on the line indention
[email protected] 47
3. Create a file called requirements.txt
$ vim requirements.txt
Add the following text in it:
flask
redis
[email protected] 49
7. Verify the application running
8. List the application running as part of the stack using compose commands
$ docker compose ps
$ docker ps
[email protected] 50
10. Re-build and run the app with Compose
$ docker compose up -d
Because the application code is now mounted into the container using a volume, we can
make changes to its code and see the changes instantly, without having to rebuild the image.
Open a new terminal and change the greeting in app.py and save it. For example, change
the Hello World! message to Hello from Docker Container!:
[email protected] 51
12. Verify the application running is updated on the fly
[email protected] 52
3. 13 CONFIGURING EXTERNAL DNS, LOGGING AND STORAGE DRIVER
[email protected] 53
$ docker exec -it nginx7 cat /etc/resolv.conf
12. Restart Docker service and verify the storage driver configured
$ sudo service docker restart
$ docker info | grep -i storage
[email protected] 55
3. 14 MULTI-STAGE DOCKERFILE
1. Clone Git
$ git clone https://github.com/k21academyuk/Multi-stage-Guide
$ cd Multi-stage-Guide/
2. Execute the Dockerfile for Single stage (note that there is a space between build and .)
docker build -f Dockerfile.singlestage -t single_stage .
[email protected] 56
3. Check Image Size
docker images
4. Execute the Dockerfile for multi stage (note that there is a space between build and .)
docker build -f Dockerfile.multistage -t multi_stage .
[email protected] 57
[email protected] 58
3. 15 MULTI-STAGE DOCKERFILE ANOTHER EXAMPLE
1. Clone Git
$ git clone https://github.com/k21academyuk/Multi-stage-Guide_1
$ cd golang-http-server/
2. Execute the Dockerfile for Single stage (note that there is a space between build and .)
docker build -f Dockerfile -t single1-small .
[email protected] 59
3. Check Image Size
docker images
4. Execute the Dockerfile for multi stage (note that there is a space between build and .)
docker build -f Dockerfile.multi -t multi1-small .
[email protected] 60
3. 16 TROUBLESHOOTING
Fix: please add coma(,) after every key: value pair like below.
[email protected] 61
5.9 Issue: Access Denied Error
Lab-6
[email protected] 62
Solution: Wrong login ID Used
Tagged image with Wrong Id
docker id: emrypala
Reason: Getting this Error because apache image not available public Docker hub
[email protected] 63
Fix: To pull http image from public repository pull httpd image
[email protected] 64
Reason: This error is coming because app.py file is wrong.
[email protected] 65
After this, follow the bellow steps.
docker ps
docker images
and delete that image
Perform docker-compose up -d again
[email protected] 66
If you still getting this Error Please watch Session 200810 – CKA – Day 5 – Docker Compose &
Hands-on Lab with Docker File Day-5 in 2008 – [Weekday] Docker and Kubernetes (CKA) –
Live Recordings
URL: https://k21academy.com/topic/200810-cka-day-5-docker-compose-hands-on-lab-with-docker-file/
[email protected] 67
5.12 Unable to Docker Login or
Create a new Docker ID
Issue: azureuser@Test:~$ docker login
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: docker_id
Password:
Error saving credentials: error storing credentials - err: exit status 1, out: `Cannot autolaunch D-Bus
without X11 $DISPLAY`
azureuser@Test:~$
Reason: This would have happened because of a glitch in the process of Docker Installation.
Fix: Look into these two links from StackOverflow (shared below) if it still doesn't work out please
uninstall and reinstall docker to fix this issue.
Links - https://stackoverflow.com/questions/50151833/cannot-login-to-docker-account
https://stackoverflow.com/questions/51222996/docker-login-fails-on-a-server-with-no-x11-installed
Issue: I installed apache2 server on containers Vol-sharing1 and 2 and was trying to access the
page but i see nginx page and not apache2 page
Reason: This might be because of the indentation issues on your docker-compose.yml file.
[email protected] 68
Fix: Please fix the indentation issues in your file docker-compose.yml and this will fix your issue.
Issue: "http://hostip:5000" I get the message "This site can’t be reached".. I opened up all ports
Fix: Please clear the cache on your browser and this will solve your issue.
Issue: Even after $ docker-compose up -d successfully the web container is is exited state.
Fix: Please make sure your docker-compose.yml indentation is proper, if not please fix it to
resolve this issue.
[email protected] 69
5.16 Runtime Error: invalid
memory address when doing
docker login
Issue: When running doker login facing below issue.
[email protected] 70
[email protected] 71
3. 17 EXTRA DOCKER QUESTION
Question: I am expected to install python I believe as the base image, with some of the steps included in
my original email, and at the end the output is suppose to give me the SHA1 value.
So after many tries, I was doing a stdin of the Dockerfile to a Dockerfile created using touch command. But
in the bash script, I 1st updated the system, installed docker, started docker, then created a Dockerfile with
the contents of the dockerfile standard input with command <<EOF. But I still didn't get the right output.
Answer:
[email protected] 72
3. 18 SUMMARY