Docker: Image

A Docker image is –

Docker Image vs Container

Let’s have a clear idea about what an image is and how it is related to a container-

Docker Image

Contains all the configurations and files required to run our application.
We can use the image, to start a container from it.

Docker Container

Running instance of a docker image.
Isolated environment for a running application.
Can be started, stopped, or restarted.
Multiple containers can be created and started using the same Docker image.

Check Images

Use the following command to get the list of all docker images on your machine-

docker images
Bash

Output:

REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
swoole1-swoole_php              latest              0f9a46af2f2a        5 days ago          568MB
swooletest-php                  latest              1eb073680413        10 days ago         595MB
filamenttest-php                latest              42a709e2d33c        11 days ago         1.32GB
bigboxcode-node                 latest              950f21ba2373        2 weeks ago         1.33GB
bigboxcode-php                  latest              a3eeff62f786        2 weeks ago         916MB
mysql                           8.0                 1eba4c9bcaa8        8 weeks ago         567MB
rabbitmq                        3-management        a4e86f36e8fd        4 months ago        251MB
Plaintext

Pull Images

We can pull images from the docker hub, by using the “docker pull” command-

docker pull ubuntu:latest
Bash

Output:

latest: Pulling from library/ubuntu
9c704ecd0c69: Pull complete
Digest: sha256:2e863c44b718727c860746568e1d54afd13b2fa71b160f5cd9058fc436217b30
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
Plaintext
docker pull mongo
Bash

Output:

Using default tag: latest
latest: Pulling from library/mongo
7646c8da3324: Already exists
d5e0f169ab67: Pull complete
1561f2bd9c09: Pull complete
ef3cb06bfe6e: Pull complete
ab3a93b1acbf: Pull complete
06bf3bf4e558: Pull complete
256b619d364b: Pull complete
8a32aa02a161: Pull complete
Digest: sha256:0cf7c3db50892da41c886e541693d6181ef77047414a8f7e15ae63e842ce65f5
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
Plaintext

Note: make sure you have docker installed on the machine.

Create Custom Image

Here is the directory structure-

Simple Docker App Directory Structure
  |
  |- Dockerfile
  |- server.js
  |

Create a file named server.js and add the following code-

// server.js

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');

  res.end('Hello, BigBoxCode!n');
});

server.listen(3000, () => {
  console.log(`Server running at http://127.0.0.1:3000`);
});
# Dockerfile

# Take node:alpine as the base image for the container
FROM node:alpine

# Copy files from the host machine to the docker container(inside /app directory)
COPY . /app

# Declare /app as the current working directory
WORKDIR /app

# Expose port 3000 on the host machine
EXPOSE 3000

# Execute command to run the server.js
CMD ["node", "server.js"]
docker build -t simpleapp .
docker images
REPOSITORY        TAG        IMAGE ID          CREATED                SIZE
simpleapp         latest     166bdff1d8a8      About a minute ago     148MB
docker run -p 3000:3000  simpleapp

# Output
# Server running at http://127.0.0.1:3000
Simpleapp docker build
Simpleapp docker build

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.