How to edit a file after I shell to a Docker container?

DockerOperating SystemOpen Source

While creating Docker images, you specify the build context for the image. The Image build context contains all the files that you want to be included inside the container that you will be from that image. This includes the source code of your application, Dockerfile, other system files, etc. Once you have specified the location of the build context using the Docker build command, you can build the image.

Moreover, you can use the COPY instruction inside a Dockerfile or even the Docker cp command to copy files inside the container from the local machine. But what if you want to edit the file inside the container? In simpler words, there might be situations when you have accessed the shell or the bash of the container and you want to edit files inside the container. In this article, we will discuss exactly how to do so.

To begin with, you need to first create an image such that when you create a container associated to that image, you have access to a text file inside the container. There are two ways to do so. Either you can directly use a COPY instruction in a Dockerfile or create a file directly inside the container.

Let’s create and run an Ubuntu container.

$ docker run -it --name=mycont ubuntu /bin/bash

In the above Docker run command, we have used the -i and -t options which stands for interactive and pseudo-TTY. These options allow us to interact with the container by providing inputs. Next, we have used the /bin/bash command which sets a default command to be executed as soon as the container is started. Thus, when we run the above command, we will have access to the bash of the ubuntu container.

You can even verify that the container is actively running by listing all the active containers. Keeping this container bash running in the terminal, open a new terminal and execute the following command.

$ docker ps

You will find that the mycont container that you created is actively running.

Once you have access to the container’s bash, you can interact with it in the same way you would interact with your Linux/Ubuntu terminal. To create a file, we can use the touch command

You can execute this command inside the container’s bash.

# touch tutorialspoint.txt

To verify the creation of the file, you can list all the contents.

# ls

If you want to edit a file inside the container, you will need access to a file editor. You can use nano or install the vim package inside the container. Before doing this, make sure you update the container

# apt-get -y update

Now, you can install the vim package using the following command.

# apt-get -y install vim

Now, open the file using the vim editor. You can use the command below to do so.

# vi tutorialspoint.txt

To insert content inside the file, you need to invoke the insert mode. Simply press the i key on your keyboard. Next, enter any content that you want. To save the content, press escape key to exit from insert mode, type :q, and press enter to save and quit.

To verify, use the cat command to print the contents inside the file.

# cat tutorialspoint.txt

In this way, you can use any editor of your choice to edit files inside the container.

If you already have a container running in the background, you can even use the Docker exec command to get access to the bash of the container.

$ docker exec -it <container-name> bash

Repeat the above steps to create and edit files.

Another way to do so and probably a better one, is to directly install the vim editor using the Dockerfile. You can do so using the RUN instruction. Let’s check it out. Consider the Dockerfile below.

FROM ubuntu:latest
COPY . .
RUN ["apt-get", "-y", "update"]
RUN ["apt-get", "install", "-y", "vim"]

By default, when you pull an Ubuntu image, you won’t find any text editor. Hence, you need to install it manually. The COPY instruction will copy all the files inside the container’s default working directory from the current directory in your host machine. Once you have executed the Docker build command on this Dockerfile to build the image, you can run a container using the Docker run command.

$ docker run -it --name=mycont <image name> bash

Doing so, you will get access to the container’s bash with vim editor already installed.

To conclude, in this article, we discussed how to create and edit a file inside a Docker container if you have access to the container’s shell. You can use the Docker run command if you don’t have a container or the Docker exec command if you already have a container running. You can install any text editor that you prefer.
Updated on 06-Aug-2021 11:58:21