Goglides Dev 🌱

Cover image for How to edit file Docker container or edit a file after Docker container?
Roshan Thapa
Roshan Thapa

Posted on

How to edit file Docker container or edit a file after Docker container?

Here are the steps for editing files in a container

  • Find the container id of a running container
  • Login inside the docker container using CONTAINER ID
  • Update the package manager
  • Install the required package vi, nano, vim etc.
  • Edit the file using either vim or nano

1. Find the container id of a running container

First, we need to find the CONTAINER ID of the running container. Use the following command to list all the running containers -

docker ps -a
The above command will list out all the running containers.

docker ps -a for view docker image before editing the file inside docker conatainer
Look at the CONTAINER IDin which you want to edit the file.

Note down or COPY the CONTAINER ID because we are going to use it to go inside the docker container.

Image description

2. Login inside the docker container using CONTAINER ID

In the previous step-1 we have to fetch the CONTAINER ID of the running container. Now we need to login into the container using the following command -

docker exec -u 0 -it 8662ea2fa000 /bin/bash

After executing the above command you will be inside your running container.

3. Update the package manager

Once you are logged into the docker container the first thing which you need to do is to update the package manager so that it will have all the latest repository URLs updated.

Run the following command to update the package manager -

# For CentOS -
yum update

# For Ubuntu
apt-get update
Enter fullscreen mode Exit fullscreen mode

4. Install the required package vi, nano, vim etc.

Now after updating the package repository you can install your favorite editor (vi, nano, vim) inside your docker container so that you can edit the file.

Here is the command for installing the editor -

# For CentOs
yum install vim-enhanced -y
yum install nano -y
yum install vim-minimal -y
#or
dnf install vim-enhanced -y
dnf install nano -y
dnf install vim-minimal -y

# For Ubuntu
apt-get install vim
apt-get install nano
Enter fullscreen mode Exit fullscreen mode

5. Edit the file using either vim or nano

Finally, you can use the command nano application.yaml or vim application.yml to edit/update your file present inside the running docker container.

Additional information

6. Install vim editor along with dockerfile

This is one of the easiest ways with which you can install your favorite editor along with your docker container. I am using the vim editor but you can choose any Linux editor of your choice.

You can write installation instructions for vim inside your Dockerfile so that whenever you built and run the docker image vim editor will always be installed inside your container.

Here is my docker file where I have installed the vim editor -

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar

RUN mkdir destination-dir-for-add
ADD sample.tar.gz /destination-dir-for-add

RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "vim"]

ENTRYPOINT ["java","-jar","/app.jar"]
BASH
If you look carefully in the above docker file then you will notice I have added only two lines of code for installing the vim into my container -

RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "vim"]
Enter fullscreen mode Exit fullscreen mode

So you can use the above two lines of codes and replace the vim with your favorite editor of your choice.

7. Using remote editor by exposing the port 22

There is one more way by which you can expose the port 22 or in other words if I say enabling the SSH into the running container. I would certainly not recommend this approach because you will start many parallel processes along with your container.

But use this approach only when you are in the development phase and where you need to debug. For a production environment, it is highly discouraged.

So here are the things which we need to do -

  1. Install openssh-server
  2. run ssh process
  3. Expose port 22
  4. Build and run docker image on port 22 We will put all this inside the docker file. Here is the docker file -
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar

RUN mkdir destination-dir-for-add
ADD sample.tar.gz /destination-dir-for-add

RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "openssh-server"]
RUN echo 'root:lollol0' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN ["/etc/init.d/ssh", "start"]

EXPOSE 22

WORKDIR "/app"

CMD ["/usr/sbin/sshd", "-D"]

ENTRYPOINT ["java","-jar","/app.jar"]
Enter fullscreen mode Exit fullscreen mode

7.1 Build the docker image
Now we need to build the docker image. Use the following command to build the docker image and substitute the image name as per your need -

docker build -t myimagewithopenssh .

7.2 Run docker image on port 22
Now in the previous two steps(Step 7, Step 7.1) we have created a dockerfile and build a docker file. Let's run the dockerfile on the port 22

Here is the command you should use for running the docker image -

docker run --rm -p 2222:22 -d --name=myimagewithopenssh myimagewithopenssh

After starting the container now you can edit the file remotely. Here is the vim command for editing the file present inside your docker container -

vim scp://root@localhost:2222//app/application.yaml

Here are some discussion threads which I found on the development forum -

Stackoverflow - How do I edit a file after I shell to a Docker container?
Docker Forum - How to edit a file in a running container?

Top comments (1)

Collapse
 
bkpandey profile image
Balkrishna Pandey

Didn't know that you can directly vim scp file, handy command.