1

My Dockerfile

FROM node:lts-alpine
RUN echo echo Hi Bob >> /etc/profile

Run

docker build -t node5 .
docker run -it --rm node5 sh

I'm expecting to see "Hi Bob" output in the terminal. It's not there.

How do on this distro do I run a command automatically when opening a terminal?

UPDATE 1

Tried this too. No joy.

RUN echo echo HI Bob >> /root/.profile

UPDATE 2

Following Joe Perri's link I find that this works using

docker run -it --rm node5 sh -l

So it just requires the terminal to be configured as a login console to work.

However opening the same container as a dev container in VsCode with the following .devcontainer.json config still fails.

{
   "build": {
      "dockerfile": "./Dockerfile"
   },
   "userEnvProbe": "loginShell"
}

My motivation is that I'm trying to construct a project such that I can spin up a stack of services in Docker containers using docker-compose up but then also connect to any of those containers using VsCode.

This mostly works. The only wrinkle is that many of these service containers will be running dev processes internally e.g. nodemon, ng serve etc, and while VsCode connects to the container ok it doesn't connect its terminal to the instance already running the dev process.

I'm trying to wire this up by launching the original dev process in a tmux session, and then automatically running a command to reconnect to that tmux session when VsCode connects to the container. This works great if I reconnect to the session manually, the only issue is that when VsCode connects to the container I can't get it to run the command to reconnect the session automatically (or any other command for that matter).

Neutrino
  • 8,496
  • 4
  • 57
  • 83
  • can I ask what's the motivation? what's your goal? just printing when executing sh? – ItayB Dec 15 '20 at 21:11
  • 1
    check out [this](https://stackoverflow.com/questions/38024160/how-to-get-etc-profile-to-run-automatically-in-alpine-docker) – Joe Dec 15 '20 at 21:17

2 Answers2

1

RUN runs when create docker image, which runs in container is CMD. So:

FROM node:lts-alpine
CMD ["echo", "Hi Bob"]

Hi Bob will print in terminal. Your Dockerfile just makes text file in docke image.

Akihito KIRISAKI
  • 1,243
  • 6
  • 12
0

what about using bash?

FROM node:lts-alpine
RUN apk add bash
RUN echo echo Hi Bob >> /etc/profile

and then

docker build -t node5 .
docker run -it --rm node5 bash

Hi Bob
bash-5.0#
ItayB
  • 10,377
  • 9
  • 50
  • 77