Start with Docker Basics(Part-2)

john sunam
3 min readAug 7, 2020

--

I am continuing this blog as the second part of the my previous docker basics blog. I will be continuing with the same project that I have used in my previous Docker Basics blog.

In my last blog I had covered basic instructions that is required to create image and we have run the container for node application. As I am continuing from the same where we have left.

So after we have run the container if we want to check out container bash terminal we can open it using below command.

$ docker exec -ti node_app_con bash

When we are able to open the container terminal you can see we are automatically into the path /usr/src/app . Why is that ? If you could remember that we have used instruction WORKDIR as /usr/src/app . So what it does is it directly takes use to the directory that we have set in our WORKDIR .

For exiting the container terminal you can just type exit command.

# exit

Now lets talk on managing the users inside container. As till now we were doing everything as root user.

For creating a new user in your container add the below line to your docker file.

RUN adduser application

This will create new user called application. After that change the ownership of you application folder to new created user.

RUN adduser application && chown application:application /usr/src/app -R

Rebuild the image after adding above line in dockerfile and run the image. After running image open the container terminal and check the ownership of the files and folder that is inside the app directory.

Now we have successfully change the ownership of the app folder. We too can switch the user within container.

User: User instruction is used to switch between the users from inside the dockerfile. For changing user you can add code below in your docker file.

USER application

After adding above line rebuild the image and run the container. After running container open the container terminal and check the user.

You can see in above screenshot that recently our user is application for that running container. We successfully have switched the user.

By now we have covered almost all of the useful instructions that can be used in docker file. In my next blog I will be talking about how we can use docker compose for running multiple container.

--

--