Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Docker running locally

 Instructions to download and install Docker

Docker Hub Search Box: Nginx  tag  Latest

docker pull nginx:latest

Running a basic web server


docker run -it --rm -d -p 8080:80 --name web nginx

http://localhost:8080
docker stop web

Adding Custom HTML


By default, Nginx looks in the /usr/share/nginx/html directory inside of the
container for files to serve.
Create a directory named site-content. In this directory add an index.html
file and add the following html to it:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Docker Nginx</title>
</head>
<body>
<h2>Hello from Nginx container</h2>
</body></html>

$ docker run -it --rm -d -p 8080:80 --name web -v


~/site-content:/usr/share/nginx/html nginx
http://localhost:8080

Build Custom NGINX Image


To build a custom image, we’ll need to create a Dockerfile and add our
commands to it.
In the same directory, create a file named Dockerfile and paste the below
commands.

FROM nginx:latest

COPY ./index.html /usr/share/nginx/html/index.html

$ docker build -t webserver .

docker run -it --rm -d -p 8080:80 --name web webserver

Setting up a reverse proxy server


git clone https://github.com/pmckeetx/docker-nginx.git

docker login
$ docker tag nginx-frontend <dockerid>/nginx-frontend

$ docker push <dockerid>/nginx-frontend

docker rmi $(docker images -q)

Create Dockerfile using this command…

We need to write some instructions in the docker file…


~vim Dockerfile~``FROM centos:7
``RUN yum install httpd -y
``COPY index.html ./var/www/html
``ENTRYPOINT ["/usr/sbin/httpd"]
``CMD ["-D","FOREGROUND"]
Save this Dockerfile using ~press esc & :wq!~

After creating a Dockerfile we need to build a docker image with the


help of the Dockerfile following this command…
``docker build -t [image Name]:[version] .``
``docker build -t dockerimage:s1 .``
1. push docker image on the docker hub using this command..

2. firstly you need to create account on the docker hub…….

3. Login docker account…..using following command…

4. write username and password your docker hub account..


''docker login''''docker push [user Name]/[image Name]:
{version]''

||User Name=docker hub account username||

If you are facing similar issue pushing docker image..

you can solve using this command..


''docker tag [image Name]:[version] [user Name]/[dockerimg]:
[version]''

||User Name=docker hub account username||


After use this command…..

“docker push [user Name]/[image Name]:{version]’’

||User Name=docker hub account username||

Now successfully pushed Docker Image on the Docker


Hub….

We can see docker images…using following command..


'docker images'

Pull Docker Image using following command..


'docker pull [user Name]/[dockerimage Name]:[version]'

||User Name=docker hub account username||


Pre-requisite Steps
 Create your Docker hub account.
 https://hub.docker.com/
 Important Note: In the below listed commands wherever you see stacksimplify you
can replace with your docker hub account id.

Step-1: Run the base Nginx container


 Access the URL http://localhost
 docker run --name mynginxdefault -p 80:80 -d nginx
 docker ps
 docker stop mynginxdefault

Step-2: Create Dockerfile and copy our customized


index.html
 Dockerfile
 FROM nginx
 COPY index.html /usr/share/nginx/html
 *index.html
 <!DOCTYPE html>
 <html>
 <body style="background-color:lightgoldenrodyellow;">
 <h1>Welcome to Stack Simplify</h1>
 <p>Docker for Beginners Demo</p>
 <p>Application Version: V1</p>
 </body>
 </html>
 Reference Link: https://github.com/stacksimplify/docker-fundamentals/tree/master/04-
Build-new-Docker-Image-and-Run-and-Push-to-DockerHub/Nginx-DockerFiles

Step-3: Build Docker Image & run it


docker build -t stacksimplify/mynginx_image1:v1 .
docker run --name mynginx1 -p 80:80 -d stacksimplify/mynginx_image1:v1

Replace your docker hub account Id


docker build -t <your-docker-hub-id>/mynginx_image1:v1 .
docker run --name mynginx1 -p 80:80 -d <your-docker-hub-id>/mynginx_image1:v1

Step-4: Tag & push the Docker image to docker hub


docker images
docker tag stacksimplify/mynginx_image1:v1 stacksimplify/mynginx_image1:v1-release
docker push stacksimplify/mynginx_image1:v1-release

Replace your docker hub account Id


docker tag <your-docker-hub-id>/mynginx_image1:v1 <your-docker-hub-id>/mynginx_image1:v1-release
docker push <your-docker-hub-id>/mynginx_image1:v1-release
Step-5: Verify the same on docker hub
 Login to docker hub and verify the image we have pushed
 Url: https://hub.docker.com/repositories

Setup environment:
$ mkdir Myapp
$ cd Myapp
Now lets create passowrd file for our DB:
$ openssl rand -base64 32 > db_password.txt
$ openssl rand -base64 32 > db_root_password.txt
Create a docker-compose.yml file:
Explain
version: '3.1'
services:
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
ports:
- "80:80"
- "443:443"
#Mysql DB
db:
image: mysql:5.7
container_name: Mysqldb
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_root_password
- db_password
secrets:
db_password:
file: db_password.txt
db_root_password:
file: db_root_password.txt

volumes:
db_data:
Create the compose container:
$ sudo docker-compose up
SSH into the instance and check the app
List out the compose services:
Explain
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
--------
Mysqldb docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp,
33060/tcp
webserver nginx -g daemon off; Up 0.0.0.0:443->443/tcp,
0.0.0.0:80->80/tcp
Verify the nginx is running:
$ curl http://localhost
Verify the Mysql db:
$ docker exec -it Mysqldb mysql -u root -p

Enter the root password which is in db_root_password.txt

Prerequisites
You will need all of the following to complete this lab:

 A Docker host

Step 1: The default bridge network


Every clean installation of Docker comes with a pre-built network
called bridge. Verify this with the docker network ls command.

Explain
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
1befe23acd58 bridge bridge local
726ead8f4e6b host host local
ef4896538cc7 none null local

The output above shows that the bridge network is associated with
the bridge driver. It’s important to note that the network and the driver are
connected, but they are not the same. In this example the network and the
driver have the same name – but they are not the same thing!

The output above also shows that the bridge network is scoped locally. This
means that the network only exists on this Docker host. This is true of all
networks using the bridge driver – the bridge driver provides single-host
networking.
All networks created with the bridge driver are based on a Linux bridge
(a.k.a. a virtual switch).

Install the brctl command and use it to list the Linux bridges on your
Docker host.

Explain
# Install the brctl tools

$ apt-get install bridge-utils


<Snip>

# List the bridges on your Docker host

$ brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242f17f89a6 no

The output above shows a single Linux bridge called docker0. This is the
bridge that was automatically created for the bridge network. You can see
that it has no interfaces currently connected to it.

You can also use the ip command to view details of the docker0 bridge.

Explain
$ ip a
<Snip>
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group
default
link/ether 02:42:f1:7f:89:a6 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:f1ff:fe7f:89a6/64 scope link
valid_lft forever preferred_lft forever

Step 2: Connect a container


The bridge network is the default network for new containers. This means
that unless you specify a different network, all new containers will be
connected to the bridge network.

Create a new container.

$ docker run -dt ubuntu sleep infinity


6dd93d6cdc806df6c7812b6202f6096e43d9a013e56e5e638ee4bfb4ae8779ce
This command will create a new container based on
the ubuntu:latest image and will run the sleep command to keep the
container running in the background. As no network was specified on
the docker run command, the container will be added to
the bridge network.

Run the brctl show command again.

$ brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242f17f89a6 no veth3a080f

Notice how the docker0 bridge now has an interface connected. This
interface connects the docker0 bridge to the new container just created.

Inspect the bridge network again to see the new container attached to it.

Explain
$ docker network inspect bridge
<Snip>
"Containers": {
"6dd93d6cdc806df6c7812b6202f6096e43d9a013e56e5e638ee4bfb4ae8779ce": {
"Name": "reverent_dubinsky",
"EndpointID": "dda76da5577960b30492fdf1526c7dd7924725e5d654bed57b44e1a6e85e956c",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
<Snip>

Step 3: Test network connectivity


The output to the previous docker network inspect command shows the
IP address of the new container. In the previous example it is “172.17.0.2”
but yours might be different.

Ping the IP address of the container from the shell prompt of your Docker
host. Remember to use the IP of the container in your environment.

Explain
$ ping 172.17.0.2
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.049 ms
64 bytes from 172.17.0.2: icmp_seq=5 ttl=64 time=0.049 ms
^C
--- 172.17.0.2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.049/0.053/0.069/0.012 ms

Press Ctrl-C to stop the ping. The replies above show that the Docker host
can ping the container over the bridge network.

Log in to the container, install the ping program and


ping www.dockercon.com.

Explain
# Get the ID of the container started in the previous step.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
6dd93d6cdc80 ubuntu "sleep infinity" 5 mins Up reverent_dubinsky

# Exec into the container


$ docker exec -it 6dd93d6cdc80 /bin/bash

# Update APT package lists and install the iputils-ping package


root@6dd93d6cdc80:/# apt-get update
<Snip>

apt-get install iputils-ping


Reading package lists... Done
<Snip>

# Ping www.dockercon.com from within the container


root@6dd93d6cdc80:/# ping www.dockercon.com
PING www.dockercon.com (104.239.220.248) 56(84) bytes of data.
64 bytes from 104.239.220.248: icmp_seq=1 ttl=39 time=93.9 ms
64 bytes from 104.239.220.248: icmp_seq=2 ttl=39 time=93.8 ms
64 bytes from 104.239.220.248: icmp_seq=3 ttl=39 time=93.8 ms
^C
--- www.dockercon.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 93.878/93.895/93.928/0.251 ms

This shows that the new container can ping the internet and therefore has a
valid and working network configuration.

Step 4: Configure NAT for external


connectivity
In this step we’ll start a new NGINX container and map port 8080 on the
Docker host to port 80 inside of the container. This means that traffic that hits
the Docker host on port 8080 will be passed on to port 80 inside the
container.

NOTE: If you start a new container from the official NGINX image without
specifying a command to run, the container will run a basic web server on
port 80.

Start a new container based off the official NGINX image.

Explain
$ docker run --name web1 -d -p 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
386a066cd84a: Pull complete
7bdb4b002d7f: Pull complete
49b006ddea70: Pull complete
Digest: sha256:9038d5645fa5fcca445d12e1b8979c87f46ca42cfb17beb1e5e093785991a639
Status: Downloaded newer image for nginx:latest
b747d43fa277ec5da4e904b932db2a3fe4047991007c2d3649e3f0c615961038

Check that the container is running and view the port mapping.

Explain
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
b747d43fa277 nginx "nginx -g 'daemon off" 3 seconds ago Up 2 seconds 443/tcp,
0.0.0.0:8080->80/tcp web1
6dd93d6cdc80 ubuntu "sleep infinity" About an hour ago Up About an hour
reverent_dubinsky

There are two containers listed in the output above. The top line shows the
new web1 container running NGINX. Take note of the command the
container is running as well as the port mapping
– 0.0.0.0:8080->80/tcp maps port 8080 on all host interfaces to port 80
inside the web1 container. This port mapping is what effectively makes the
containers web service accessible from external sources (via the Docker hosts
IP address on port 8080).

Now that the container is running and mapped to a port on a host interface
you can test connectivity to the NGINX web server.

To complete the following task you will need the IP address of your Docker
host. This will need to be an IP address that you can reach (e.g. if your lab is
in AWS this will need to be the instance’s Public IP).
Point your web browser to the IP and port 8080 of your Docker host. The
following example shows a web browser pointed to 52.213.169.69:8080

You might also like