Docker images

Alex Izuka
4 min readMar 13, 2022

Today we would be looking at Docker images and how to push images created to docker hub. In our last article, we introduced docker kindly click this link https://aleqxan.medium.com/getting-started-with-docker-49c8f178af3f as it would help with understanding what would be doing in this article.

What are Docker Images?

A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker server. It provides a convenient way to package up applications and preconfigured server environments, which you can use for your own private use or share publicly with other Docker users. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Also, docker image is a lightweight, executable package that contains the libraries needed to run software on it.

Building a Docker Image

They are two ways we can build our docker image;

→ Running a Docker container; involves running a docker container and placing all your code inside.

→ Build codes locally(on your computer), then package all codes into a docker image using a dockerfile

In this article, we would be going with the section where codes are built locally, packaged into docker images.

Create A Simple NodeJS

We have created a NodeJS application here https://github.com/sfxgizuka/mongodb-exores If you have no prior knowledge of what NodeJS you should check this https://nodejs.org/en/

Create a Dockerfile; Create an empty Dockerfile touch Dockerfile You can use any editor but I usually love the Vs code editor

Using the FROM command, we can mention which software is needed to run the application. The FROM command will install the mentioned software. It is a NodeJS application. So we need NodeJS. Here I used FROM node:16 The FROM command installs Node inside the image.

The RUN command is used to run the Linux commands. You can use multiple RUN commands. Here we are creating a new directory /home/app using the RUN command.

The RUN command is executed inside the docker image. Not in the local computer.

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

4. The COPY command is used to copy files from the local computer to the Docker image. The first parameter is the local computer file path. The second parameter is the Docker image file path.

COPY package*.json ./
USER node

5. Install npm packages using the npm install command. Here we used another RUN command to execute the npm command.

npm install

6. The Expose & CMD command is used to expose port and start the application.

EXPOSE 3000

CMD [ "node", "app.js" ]

Summing this up your dockerfile should look like this;

FROM node:17

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

WORKDIR /home/node/app

COPY package*.json ./

USER node

RUN npm install

EXPOSE 3000

CMD [ "node", "app.js" ]

Build image

Building images after what we have done above is quite easy, you just need to do the following on your command-line interface;

docker build -t (your_dockerhub_username)/(image_name)

To see your newly built image you run this command;

docker images

You create a new container with this image when you do docker run, so you can run your image in a container by doing the following;

docker run — name (container_name) -p 3000:3000 -d (your_dockerhub_username)/(image_name)

“-p” used in the command means the command will create all the directories necessary to fulfill your request, not returning any error in case that directory exists.

“-d”, detached mode, used in the command means that the Docker container runs in the background of your terminal. It does not receive input or display output.

To confirm your container is running on your local, enter the command;

docker ps

To test your image is active, kindly use your container IP, input on your browser and run, it should show you your page

Docker registry

After building our image and seeing its running as a container it is important we push them to dockerhub(where images are stored), to do that for our newly built image we would do the following;

  • Login to your dockerhub account using this command;
docker login -u (your_dockerhub_username)

The “-u” refers to you the user.

  • Push your local image into dockerhub; after login, you then push your image to dockerhub with this command;
docker push (your_dockerhub_username>>/<<your-node-image)

By now you have built a docker image for a node application, run as a container, confirmed it’s running on your web and pushed to docker registry, you can use this method for your practise sessions or your projects.

Kindly check out our previous articles aleqxan.medium.com

--

--