Building a custom docker container image and pushing it to Docker Hub – Docker Compose
Building a custom docker
container image and pushing it to Docker Hub – Docker Compose
1, Install Docker in Ubuntu
Linux using snap
$ sudo apt update
If your system doesn’t have
snap then install snapd first
$ sudo apt install snapd
Install docker
$ sudo snap install docker
2, Create Dockerfile which
contains sets of code in specific order,
docker build command creates a custom image
Sample Dockerfile is as follows,
nginx webserver
----------------------------------------------
FROM nginx
COPY index.html
/usr/share/nginx/html/
-----------------------------------------------
Folder structure looks like
the below,
index.html file can be as simple as “This is my first web page”
-----------------------------------------------
ubuntu@ip-172-31-93-142:~/docker-compose-example$
ls -l
total 8
-rw-rw-r-- 1 ubuntu
ubuntu 50 Oct 23 22:27 Dockerfile
-rw-rw-r-- 1 ubuntu ubuntu
423 Oct 23 22:26 index.html
ubuntu@ip-172-31-93-142:~/docker-compose-example$
-----------------------------------------------
3, Build Docker image and
push it to Docker Hub
$ sudo docker build . -t
nginx-webapp ### This command builds a
custom docker image “nginx-webapp” by reading the Dockerfile
$ sudo docker run -itd -p
3333:80 nginx-webapp ### This command
creates a docker container from image “nginx-webapp” and expose
the container on port 3333
$ curl http://localhost:3333 ### This command will now display the
custom website for you to verify
$ sudo docker tag nginx-webapp abushad/nginx-webapp ### This command creates a
new tag for your image so that its compatible with your docker hub and
ready to
be pushed to docker hub , Format of tag is “dockerhubid/imagename”
$ sudo docker login ### Use your username and password to login to your docker
hub before you can push your
docker image
$ sudo docker push abushad/nginx-webapp ###Finally this will now push
your docker image to docker hub
Comments
Post a Comment