0

I created Dockerfile with the following content,

FROM node:16.4.2-alpine3.14

WORKDIR /app

COPY package.json .

COPY . /app

then I created image,

docker build -t app:0.1 .

and then started the container by running,

docker run -it app:0.1

It opened the node shell.

I then closed it.

Doing docker ps -a gives the following output,

enter image description here


Now,

I want to restart the same container and with node shell. How can it be done?

3 Answers 3

2

It shows as exited since there is no process running. To start it again , you can use the docker start command with -i flag. https://docs.docker.com/engine/reference/commandline/start/ for more options

0
1

docker rm the existing container and docker run a new one. Consider using the docker run --rm option so the container deletes itself when it's done.

There's nothing special or valuable about a container; it's just a wrapper around a single process (in your case the Node REPL), and creating a new one isn't especially expensive. In the same way that you can't restart the REPL once you've exited it but need to re-run node, you generally will want to delete and recreate containers once their process has finished. With longer-running processes this also helps ensure the process's filesystem is exactly what you expect: if something exits unexpectedly, deleting the container will also remove any temporary files or lock files it's left behind, so restarting the container will run successfully.

1
  • This statement, There's nothing special or valuable about a container; it's just a wrapper around a single process (in your case the Node REPL), and creating a new one isn't especially expensive resolves lots of my doubt. Thanks. Jul 11, 2021 at 11:17
1

use the following sequence of commands:

docker container start magical_merkle
docker attach magical_merkle

Explanation: the first command restarts your exited container, but in detached mode, it means it's running in the background and you can't see it's output. now for you to reattach to the container you run attach command of docker (second command) which attaches the std io of your host terminal to the std io of the running container.

you may notice the magical_merkle in the commands. this is the name of your container as found in the container ls output you provided. when you run the run command, docker will name the container with a auto generated name if you don't provide one for it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.