farmer1010
farmer1010

Reputation: 81

Stopping and running Docker container doesn't update

I am trying to learn Docker. Maybe I am over complicating it. I have built a Docker Container with Nginx and am just trying to build a static HTML Website. I originally only put Hello World in my HTML file and ran the container. It showed up just find in browser. I then stopped the container and added a lot to the html file. However, when I start the container back up, nothing has changed and the Hello World is the only thing that shows in browser. I guess I'm not understanding why it is not changing along with my HTML. Any help would be greatly appreciated.

I am stopping the container with: $ docker stop web

and I am starting it back up with: $ docker run -it --rm -d -p 8080:80 --name web webserver

when I stop it and docker ps it does not show up

Also, using docker restart doesn't really seem to do anything at all

Upvotes: 2

Views: 1765

Answers (4)

anatolhiman
anatolhiman

Reputation: 1859

This typically happens when the Docker container isn't aware of where you make changes outside of the container. The code in the container is a copy of the code you have in your project, it is NOT the code itself. You thus have to tell Docker somehow that "this is my codebase here, and I want you to copy that into your container". For this you use volumes. I do this with Docker-compose and specify the volumes like this:

    volumes:
      - .:/usr/src/service
    working_dir: /usr/src/service

The . dot means your working directory and what is on the other side of the : is where your working directory is copied into – in this case /usr/src/service where Docker keeps its containerized services (on unix systems (Mac, Linux), at least, I'm not sure if this applies to windows).

The working_dir command tells Docker to listen for changes on stuff that happens in the service directory inside of the container.

If you now run your app and make a change, and have a live server type of service like Nodemon etc. running in the working directory, it would normally pick up those changes and display your edited html content.

If you have made changes to your Dockerfile, package.json or other "build files" you will have to run docker-compose up --build in order to get those changes into the container. If you just change regular source code you run docker-compose up only.

Upvotes: 1

Eduardo Fernando
Eduardo Fernando

Reputation: 629

Once a dockerfile is built, it's containt is not mutable anymore. So there are some other paths you could take to reach your goal.

  1. You can rebuild the image and re-run it. (Maybe the best case to put in production)
  2. You can build the image to execute an file that is binded dynamically on your docker run.

To follow the second path:

  1. Write a image to execute your ./index.html without copying it on dockerfile.
  2. docker run -it -v <HTML_LOCAL_PATH>:<HTML_CONTAINER> --rm -d -p 8080:80 --name web webserver

Upvotes: 0

CharlesC
CharlesC

Reputation: 39

Where did you modify the HTML file? It seems like the HTML file which is inside the container is not updated.

docker ps command only show the running containers. You can use docker ps -a to see all the containers including stopped containers.

Upvotes: 0

Ozone
Ozone

Reputation: 1353

Docker containers are meant to be immutable, devoted to running a single, particular process. Images should be built and run and you should not change the content of the container.

So in best practise for production, you would create a new container based on image with your code/updates attached.

Though I am aware there are use cases to modify a running container, it wildly depends on which image you use and how well the resulting container handles live changes (does it cache?).

Upvotes: 0

Related Questions