Hello to all the readers!
In this post I will be creating a docker image out of the simple tutorial posted on Docker’s documentation.
Let us begin with the steps.
Clone the repository
git clone https://github.com/docker/getting-started.git
cd getting-started/app
Create Dockerfile. Dockerfile is a set of instructions to build the docker image.
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
Next is to build the image.
docker build -t getting-started .
Sending build context to Docker daemon 4.626MB
Step 1/6 : FROM node:18-alpine
18-alpine: Pulling from library/node
31e352740f53: Pull complete
560412e561fb: Pull complete
02735cb6c78b: Pull complete
86d562f7b855: Pull complete
Digest: sha256:8942c014f3c3c445bb74f2ae6dfe0a024fe4b56990dabfcd87c06134d9797f98
Status: Downloaded newer image for node:18-alpine
---> f85482183a4f
Step 2/6 : WORKDIR /app
---> Running in 664ea2f0964b
Removing intermediate container 664ea2f0964b
---> ae0ad6105707
Step 3/6 : COPY . .
---> f53d252cebee
Step 4/6 : RUN yarn install --production
---> Running in b13e265f6d73
yarn install v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 12.08s.
Removing intermediate container b13e265f6d73
---> 7f2048e66dd4
Step 5/6 : CMD ["node", "src/index.js"]
---> Running in a0399302bb39
Removing intermediate container a0399302bb39
---> 7ed5a668000d
Step 6/6 : EXPOSE 3000
---> Running in 140d45449049
Removing intermediate container 140d45449049
---> 52ed8b2d5023
Successfully built 52ed8b2d5023
Successfully tagged getting-started:latest
This builds the docker image. Let us see it in the list of images.
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
getting-started latest 52ed8b2d5023 53 seconds ago 265MB
Let us now start the docker container out of the above created image.
docker run -dp 3000:3000 getting-started
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e287c2bc2e4 getting-started "docker-entrypoint.s…" 7 seconds ago Up 5 seconds 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp zen_khayyam
This is a sample todo application written in Node.js and we can now access it on localhost:3000. Option “-d” means to run the container in detached mode in background.
There is another way to list the docker containers.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e287c2bc2e4 getting-started "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp zen_khayyam
Now we can stop and remove the container
docker stop 0e287c2bc2e4
docker rm 0e287c2bc2e4
Container is now not available to be started again. It won’t be found in “docker container ls –all”.