2

I am trying to run a container and modify certain files in it. I am trying to do this using a script. If I use: docker run -i -t <container> <image>, it is giving me

STDERR: cannot enable tty mode on non tty input

If I use: docker run -d <container> <image> bash, the container is not starting.

Is there anyway to do this?

Thanks

2 Answers 2

1

Run the docker image in background using:

docker run -d <image>:<version>

Check running docker containers using:

docker ps

If there is only one container running you can use below command to attach to a running docker container and use bash to browser files/directories inside container:

docker exec -it $(docker ps -q) bash

You can then modify/edit any file you want and restart the container.

To stop a running container:

docker stop $(docker ps -q)

To run a stopped container:

docker start -ia $(docker ps -lq)
Sign up to request clarification or add additional context in comments.

5 Comments

and if you want to keep those modifications, you will have to docker commit the doc docs.docker.com/reference/commandline/cli/#commit
@anubhava you can't use docker exec from a script. I've already tried. It gives me STDERR: cannot enable tty mode on non tty input error. I think the bestway is share volumes between container and host. But when I'm sharing a directory between host and container, the files in the container folder are getting replaced by the files in the host. If the host directory is empty then all my files in the container are lost. Is there any way to share container files into host. thanks
I am already using docker exec from inside shell scripts. Show your script by editing your question so that I can investigate it.
I am trying this in script docker run --name container-name -v /home/my_files/:/home/container/files -d <image> this command is doing nothing. Moreover if I need to edit certain configuration files before I start my tomcat in container, what is the best way to do this.
What do you mean by this command is doing nothing? Isn't docker run starting your container?
0

So to start off, the -i -t is for an interactive tty mode for interacting with the container. If you are invoking this in a script then it's likely that this won't work as you expect.

This is not really the way containers are meant to be used. If it is a permanent change, you should be rebuilding the image and using that for the container.

However, if you want to make changes to files that are reflected in the container, you could consider using volumes to mount directories from the host into the container. This would look something like:

docker run -v /some/host/dir:/some/container/dir -d container

At this point anything you change within /some/host/dir will be within the container at /some/container/dir. You can then make your changes with a script on the host, without having to invoke the docker cli.

4 Comments

I've already tried this. When I run `docker run -v /some/host/dir:/some/container/dir --name <some-name> -d <image>'. Docker is not running the container in the foreground. Instead the container exited. As the container exited I was not able to use mounted. volumes.
can you show a reproducer ? the complete command used to launch the container, what docker eventsshows when you container fails?
@AkkarinZA that runs fine now, the real issue is when I'm sharing a directory between host and container, the files in the container folder are getting replaced by the files in the host. If the host directory is empty then all my files in the container are lost. Is there any way to share container files into host. I need to edit files in container from host not the other way. thanks.
The -d means that it is running in 'detached' mode - the background. It is exiting because the process that the container is running has exited (or finished). I'm not sure if docker containers are meant to be edited from the host in the way that you are hoping. You may have to have the container expose a service which makes the modifications you want, and have the script use that service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.