raghav
raghav

Reputation: 451

Docker container restarting on defining an entrypoint

I'm trying to run a shell script as entrypoint in my docker container. For this, I defined an entrypoint in my Dockerfile like this

abc.Dockerfile

FROM ubuntu
COPY abc.sh /
RUN chmod +x /abc.sh
ENTRYPOINT /abc.sh

Dockerfile gets built successfully, but on doing exec into the container I'm getting an error:

$ docker exec -it drupaldocker_myapp_1 bash
Error response from daemon: Container ca32bafe2247e51d5e9b1a8f0cf83457ccecb29e8ff9747a8c98aa6a046b0550 is restarting, wait until the container is running

Ouput of docker ps:

Raghav-Macbook:iflychat-server-development-system raghav$ docker ps
CONTAINER ID        IMAGE                COMMAND                    CREATED             STATUS                         PORTS                    NAMES
ca32bafe2247        drupaldocker_myapp   "/bin/sh -c \"/abc.sh\""   9 minutes ago       Restarting (0) 2 minutes ago                            drupaldocker_myapp_1

My docker-compose.yml looks like

version: '2'
services:
  myapp:
   build:
    context: .
    dockerfile: abc.Dockerfile
   restart: always
   tty: true

abc.sh

#!bin/sh

ls
cat /abc.sh

Also, docker-compose up is giving me no error. Output looks like

Raghav-Macbook:drupal-docker raghav$ docker-compose up
Recreating drupaldocker_myapp_1
Attaching to drupaldocker_myapp_1 
myapp_1  | oot  etc   lib    media  opt root  sbin  sys  usr
myapp_1  | bin  dev   home  lib64  mnt    proc  run   srv   tmp  var
myapp_1  | #!bin/sh 
myapp_1  | 
myapp_1  | ls

What changes should I do to make this work?

EDIT

Output of docker logs drupaldocker_myapp_1

abc.sh  boot  etc   lib    media  opt   root  sbin  sys  usr
bin dev   home  lib64  mnt    proc  run   srv   tmp  var 
#!bin/sh

ls
cat /abc.shabc.sh   boot  etc   lib    media  opt   root  sbin  sys  usr
bin dev   home  lib64  mnt    proc  run   srv   tmp  var
#!bin/sh

ls
cat /abc.sh

EDIT 2

Does a container gets stopped once the entrypoint script is executed?

Upvotes: 3

Views: 4211

Answers (1)

OscarAkaElvis
OscarAkaElvis

Reputation: 5714

I think what you want is docker run and not docker exec command. Docker exec is to launch something on an already deployed up and running container... and what you want is to launch a script once upon deployed.

Modify your Dockerfile. Use this instead of entrypoint on last line:

CMD ["bash", "-c", "/abc.sh"]

Use this command to run it and the script will be launched automatically:

docker run -it drupaldocker_myapp_1

If you put bash at the end of the docker run command you'll get an interactive bash console inside the container but the automatic script launching is avoided:

docker run -it drupaldocker_myapp_1 bash

Answering to your question "Does a container gets stopped once the entrypoint script is executed?" the answer is yes. If the entrypoint script is executed and there is nothing more to be executed, the container stops because it has nothing more to do. If you want a container to be kept up and running you should run inside it something with no immediate end like a script.

Hope it helps.

Upvotes: 5

Related Questions